Trying to get node name of childs children
I am currently experiencing difficulty iterating through an xml DOM (using MSXML 4.0) and extracting all the node names. I am able to pull the node names for the immediate children of the parent of the document but when it comes to children of the child nodes I just get the text value. Here is the code plus a sample of the xml doc. If anyone has any pointers it would be greatly appreciated. For example the children of "contributor". Basically I want to return all the node names from the xml file.
---------------------Code--------------------------------------------
Dim xDoc As New MSXML2.DOMDocument40
xDoc.async = False
xDoc.validateOnParse = True
If xDoc.Load("C:\rh_.xml") = False Then
MsgBox "Failed to load xml data from file." & vbCrLf & _
"Line: " & xDoc.parseError.Line & vbCrLf & _
"Element: " & xDoc.parseError.srcText & vbCrLf & _
"Reason: " & xDoc.parseError.reason
Exit Sub
Else
Dim strOut As String
strOut = ""
Dim oNodes As IXMLDOMNodeList
Dim oNode As IXMLDOMNode
Set oNodes = xDoc.selectNodes("//product/*")
For i = 0 To oNodes.length - 1
Set oNode = oNodes.nextNode
If Not (oNode Is Nothing) Then
If sName <> "othertext" Or sName <> "contributor" Then
sNode = oNode.xml
sName = oNode.nodeName
sValue = oNode.Text
strOut = strOut _
& "Node (" & CStr(i) & "), <" & sName & ">:" _
& vbCrLf & vbTab & "Value: " & sValue & vbCrLf
ElseIf sName = "contributor" Then
sNode = Node.nodeName
End If
End If
Next
MsgBox "File" & vbCrLf & _
strOut
End If
Set xDoc = Nothing
Set oNode = Nothing
Set oNodes = Nothing
End Sub
----------------------------- End of Code --------------------------
------------------------------ File --------------------------------
<ONIXmessage>
<header>
<m173>2013975</m173>
<m174>Random House</m174>
<m175>Sam Baum</m175>
<m180>44</m180>
<m182>200306221017</m182>
<m183>FULLCAT; Delta product extraction</m183>
</header>
<product>
<a001>076150186X</a001>
<a002>04</a002>
<b004>076150186X</b004>
<b005>9780761501862</b005>
<b012>BC</b012>
<b028>The Einstein Factor</b028>
<b029>A Proven New Method for Increasing Your Intelligence</b029>
<contributor>
<b034>1</b034>
<b035>A01</b035>
<b036>Win Wenger, Ph.D.</b036>
<b037>WENGER, WIN PHD</b037>
<b039>Win</b039>
<b040>Wenger</b040>
<b043>Ph.D.</b043>
</contributor>
<b059>eng</b059>
<b061>352</b061>
<b064>PSY000000</b064>
<othertext>
<d102>13</d102>
<d103>02</d103>
<d104></d104>
</othertext>
<othertext>
<d102>17</d102>
<d103>02</d103>
<d104></d104>
</othertext>
<othertext>
<d102>08</d102>
<d103>02</d103>
<d104></d104>
</othertext>
<othertext>
<d102>04</d102>
<d103>02</d103>
<d104></d104>
</othertext>
<b079>Prima Lifestyles</b079>
<imprint>
<b241>02</b241>
<b243>PL</b243>
<b079>Prima Lifestyles</b079>
</imprint>
<b081>Crown Publishing Group</b081>
<b003>19951018</b003>
<supplydetail>
<j136>2013975</j136>
<j137>Random House</j137>
<j138>CA</j138>
<j138>US</j138>
<j141>IP</j141>
<j143>19951018</j143>
<j145>24</j145>
<price>
<j148>01</j148>
<j151>25.95</j151>
<j152>CAD</j152>
</price>
<price>
<j148>01</j148>
<j151>16.95</j151>
<j152>USD</j152>
</price>
</supplydetail>
</product>
</ONIXmessage>
---------------------------- End of File ----------------------------
|