|
-
May 29th, 2004, 08:51 PM
#1
Thread Starter
Fanatic Member
Need a hand with XML, please [RSLVD]
Allright this is an easy one for all who have worked much with XML. I understand the structure, I think.. but I'm having issues with the syntax. I tried sniffing around this forum with no luck. The examples don't have what I'm looking for, are too complex, or I am generaly blind.
I have a file such as this one called Commands.xml
Code:
<FUNCTIONS>
<COMMAND>
<WHOIS>
<NAME Tag="Max">That is me</NAME>
</WHOIS>
</COMMAND>
All I want to do, is create a domDocument (done) and read the first element.. which is COMMAND. After that, I want to cycle through all the children of COMMAND incase there are more NAME elements.
My code:
Code:
'This is a type I have created
Private Type xmlCommandTree
docFile As New DOMDocument30
docParent As IXMLDOMElement
docCommand As IXMLDOMElement
docName As IXMLDOMElement
End Type
------------------------------------------
'Actuall function
Dim xCommand As xmlCommandTree
With xCommand
.docFile.Load "\commands.xml"
Set .docCommand = .docFile.firstChild
Set .docParent = .docCommand.firstChild
'Here I would put in a loop that cycles through all the children
'However, when I run this piece of code it sais:
'Object variable or With Block not set - Whats that mean?
End With
Can someone give me a hand before all my hair turns gray.
Thanks
Last edited by invitro; May 30th, 2004 at 02:29 PM.
ok, so... windows takes 1 minute to search for a file on my PC yet google.com takes 1 second to search the entire internet? 
-
May 30th, 2004, 12:44 AM
#2
I don't understand what you are doing with the UDT but you can do it this way.
VB Code:
Dim docFile As New DOMDocument30
Dim docParent As IXMLDOMNode
Dim docName As IXMLDOMNodeList
Dim intIndex As Integer
docFile.Load "C:\temp\test.xml"
Set docParent = docFile.documentElement.selectSingleNode("COMMAND")
Set docName = docParent.selectNodes("./WHOIS/NAME") ' The ./ says look starting at the current node
For intIndex = 0 To docName.length
Debug.Print docName(intIndex).Text
Next
Set docParent = Nothing
Set docName = Nothing
-
May 30th, 2004, 06:39 AM
#3
Hyperactive Member
I have a program that manipulates a large XML file but it doesn't really care about what is in the file other than this snippet of code:
VB Code:
Set root = xmlDoc.selectSingleNode("/ZoneLabsSettings/ruleset/zones/restricted")
Set oNodeList = root.selectNodes("*")
For Index = 0 To (oNodeList.length - 1)
Set NewNode = oNodeList.nextNode
If InStr(NewNode.getAttribute("description"), Chr(232) & Chr(255)) Then
root.removeChild NewNode
End If
Next Index
It searches through the children of a node and deletes the ones that match with a certain two characters in their string.
VB.NET 2005 Express with .Net 2.0
C# 2010 .Net 4.0
-
May 30th, 2004, 12:30 PM
#4
Thread Starter
Fanatic Member
the UDT is just straight outa my code. I put it there to make things easier.. but your code is totaly valid, and easier to read as well. 
So what I dont understand then, is the NODE part.
I thought anything inside of <> was an element. Therefore, a statement like:
<test>
<test2>
</test2>
<test>
would be an ELEMENT inside of an ELEMENT. Therefore, I thought you could simply GET the ELEMNT of an ELEMNT. Could you please explain why use a node, and why my code woulden't work?
Thanks for the replies guys, great work.
ok, so... windows takes 1 minute to search for a file on my PC yet google.com takes 1 second to search the entire internet? 
-
May 30th, 2004, 01:08 PM
#5
An element is a type of node and the element class inherits a bunch of stuff from the node class. I don't believe there is a lot of difference between the two and I have just always used nodes.
-
May 30th, 2004, 01:49 PM
#6
Thread Starter
Fanatic Member
I see... wonder why my code block does not work?
O well, I will go try your method. Thanks again!
ok, so... windows takes 1 minute to search for a file on my PC yet google.com takes 1 second to search the entire internet? 
-
May 30th, 2004, 02:29 PM
#7
Thread Starter
Fanatic Member
Ok I feel really dumb. My first example did infact work, however.... the filepath was pointing at the wrong file so it was opening a blank document. So it gave me the same error when i tried your method. Sheesh, now i feel dumb.
Ooops, thanks a lot guys.
ok, so... windows takes 1 minute to search for a file on my PC yet google.com takes 1 second to search the entire internet? 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|