Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 1st, 2003, 03:07 PM
Registered User
 
Join Date: Aug 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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 ----------------------------

 
Old August 1st, 2003, 03:11 PM
Registered User
 
Join Date: Aug 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One further note... Please ignore the operational logic in the if statement that contains othertext and contributor... I forgot to take it out. Proper real code looks like this:

Dim xDoc As New MSXML2.DOMDocument40
    xDoc.async = False
    xDoc.validateOnParse = True

    If xDoc.Load("C:\Documents and Settings\MMuffett\My Documents\Development\Onix\rh_Onix.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
            sValue = oNode.Text
            sNode = oNode.xml
            sName = oNode.nodeName
            strOut = strOut _
            & "Node (" & CStr(i) & "), <" & sName & ">:" _
            & vbCrLf & vbTab & "Value: " & sValue & vbCrLf
     End If
     Next
        MsgBox "Onix" & vbCrLf & _
        strOut
    End If

    Set xDoc = Nothing
    Set oNode = Nothing
    Set oNodes = Nothing
End Sub







Similar Threads
Thread Thread Starter Forum Replies Last Post
The reference node is not a child of this node.XSL XMLUser XSLT 2 February 25th, 2008 05:22 AM
Copy parent node and not its children bonekrusher XSLT 4 August 29th, 2007 08:44 AM
Copying Source Node attributes to output node pvsat XSLT 2 November 3rd, 2005 09:46 AM
get node & childs in a DB Tazu XSLT 2 September 29th, 2005 10:20 AM
Counting node-set children in template rufustfirefly XSLT 2 May 3rd, 2004 08:48 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.