1

For example, I want to loop through only the childnode of "test" with the tag name "result".

  <test>

    <result>
    </result>
    <memo1>
    </memo1>

    <result>
    </result>

    <memo2>
    </memo2>

    <result>
    </result>

    <memo3>
    </memo3>


  </test>

I thought this might provide what I want. However, I don't exactly get how it works(if it indeed provids what I want).

Community
  • 1
  • 1
Aqqqq
  • 816
  • 2
  • 10
  • 27
  • what is it that you are trying to do? the xml data that you provided is very limited. please put in another level of child nodes, so that a more informative answer can be written using your data. – jsotola Aug 08 '17 at 16:02

1 Answers1

1

Here's an example of using XML data. I'm fairly certain your XML file has to have <?xml version='1.0'?> on the first line. But apart from that, this is how it works:

Sub testXMLLoop()
    Dim xml As String
    xml = "<?xml version='1.0'?>" & _
    vbCrLf & "  <test>" & _
    vbCrLf & "    <result>" & _
    vbCrLf & "    </result>" & _
    vbCrLf & "    <memo1>" & _
    vbCrLf & "    </memo1>" & _
    vbCrLf & "    <result>" & _
    vbCrLf & "    </result>" & _
    vbCrLf & "    <memo2>" & _
    vbCrLf & "    </memo2>" & _
    vbCrLf & "    <result>" & _
    vbCrLf & "    </result>" & _
    vbCrLf & "    <memo3>" & _
    vbCrLf & "    </memo3>" & _
    vbCrLf & "  </test>"
    Dim xmlDoc As Object
    Set xmlDoc = CreateObject("Msxml2.DOMDocument.6.0")
    xmlDoc.LoadXML xml
    'xmlDoc.Load "file\path\to\books.xml"
    If (xmlDoc.parseError.ErrorCode <> 0) Then
        Dim myErr As Object
        Set myErr = xmlDoc.parseError
        Debug.Print "You have error " + myErr.reason
    Else
        Dim objNodeList As Object
        Set objNodeList = xmlDoc.getElementsByTagName("test")
        Dim oTestList, oChild As Object
        For Each oTestList In objNodeList
            For Each oChild In oTestList.ChildNodes
                if oChild.nodeName = "result" then
                    'Do stuff with oChild
                    Debug.Print oChild.nodeTypedValue
                end if
            Next
        Next
    End If
End Sub
Sancarn
  • 2,575
  • 20
  • 45