|
-
Aug 4th, 2005, 11:06 AM
#1
Thread Starter
Addicted Member
read xml
hi gurus
New to xml so go easy on me. I know that for some of you this is no problem.
I have an xml file:
<root>
<social>xxx-xx-xxxx</social>
<color>black and white</color>
<name>
<first> john</first>
<Last> smith</Last>
</name>
</root>
I need to read the xml file and browse through all the nodes so I can get values on specific rules
Need your help thanks
so far I have the following:
Code:
Dim nodelist As MSXML.IXMLDOMNodeList
Dim rootele As MSXML.IXMLDOMElement
Dim xNode As MSXML.IXMLDOMNode
Set xDoc = New MSXML.DOMDocument
If xDoc.Load("C:\SHC_2005_07_28_16_10_59_165_SINGLE.xml") Then
MsgBox "loaded"
Set rootele = xDoc.documentElement
Set nodelist = rootele.childNodes
For Each xNode In nodelist
'do something
next xNode
end if
hoW do I browse through the child to get values.
the code above gives me the childs of the root (social,color,name) but it does not bring up the child of name (first,last)
Thanks again in advance
-
Aug 4th, 2005, 11:22 AM
#2
Re: read xml
I think you need to enumerate through the children of each node you are enumerating 
VB Code:
Dim nodelist2 As IXMLDOMNodeList
For Each xNode In nodeList
Set nodelist2 = xNode.childNodes
For Each xNode In nodelist2
' etc...
Off the top of my head right now I can't think of a better way, but I'm sure there is one...
-
Aug 4th, 2005, 12:31 PM
#3
Re: read xml
Gaaaah.... you would tax my brain like this....
OK, this is all from the memory banks, so I cannot guarantee the accuracy of it 100% but it should be the 99% solution to get you going (if nothing else an idea of what to search on.)
Ok, here's the skinny.
THis right here:
Set nodelist = rootele.childNodes
That gets the children of the root node tag. Currently that is: social, color, and name. It does NOT include first or Last because they are children of the name node.
OK, so your loop is good for the first level, but we want to dig deeper. Every node has a property called .hasChild (it might be .hasChildren - in either case it's a boolean). You can't check .childNodes.Length, because if it doesn't have children, it will be NULL and generates all kind of errors about objects not instaciated, blah, blah, blah.
Ok, so once you determine that the current node you are looking at hasChildren then you can set another nodelist variable to those children and iterate through them as well. If you are only needing to go two levels deep, this should be OK. If you plan on going any further, you may want to consider either a recursive function that takes the childNodes as a parameter, or possibly learn a little about the .selectNodes/.selectSingleNode functions. They are a bit more complex, but very powerful in being able to query out specific nodes that fit criteria (it's kinda like an XML version of SQL).
Tg
-
Aug 4th, 2005, 02:00 PM
#4
Thread Starter
Addicted Member
Re: read xml
Thanks guys, thanks a lot, I will check into it
Thanks again
-
Aug 4th, 2005, 03:06 PM
#5
Thread Starter
Addicted Member
Re: read xml
tg
Any code that I can use as sample. on recursive funtion or selectnode,selectsinglenode
or any link that can show me how?
thanks
-
Aug 4th, 2005, 05:53 PM
#6
Re: read xml
Let me get back to you..... I know I used to... but that was beforethe pc melt down of May05... I'll see if I can dig something up.
Tg
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
|