Search notes:

VBA: MSXML and DOM

Recursively iterate over XML document

The following Visual Basic for Application code snippet attempts to demonstrate how it is possible to recursively parse the nodes of an XML document.
option explicit

sub main() ' {

   dim doc as new MSXML2.DOMDocument

   doc.loadXML ("<elemRoot>"                                  & _
                "  <child id='one' flagged='yes'>foo</child>" & _
                "  <!-- comment -->"                          & _
                "  <child id='two'>"                          & _
                "      pre bar"                               & _
                "     <grand-child>ABC</grand-child>"         & _
                "      post bar"                              & _
                "  </child>"                                  & _
                "  <child id='three'>baz</child>"             & _
                "</elemRoot>")

   call iterateRecursively(doc.childNodes(0), 0)

end sub ' }

sub iterateRecursively(byRef node as variant, byVal level as long) ' {

    dim txt as string

    if node.nodeType = MSXML2.NODE_ELEMENT then
         txt = node.nodeName
    else
         txt = node.text
    end if

    debug.print (space$(level * 4) & node.nodeTypeString & ": " & txt)

    dim childNode as variant

    if node.nodeType = MSXML2.NODE_ELEMENT then

       dim attr as MSXML2.IXMLDOMAttribute
       for each attr in node.attributes
           debug.print (space$(level * 4 + 2) & attr.Name & " = " & attr.value)
       next attr

       for each childNode in node.childNodes
           call iterateRecursively(childNode, level + 1)
       next childNode

    end if

end sub ' }
Github repository about-VBA, path: /object-libraries/MSXML/DOM/iterate-recursively.bas

See also

Using parseError to check if an XML document is valid.
selectSingleNode(query) allows the evaluate an XPath expression on an XML document.
selectNodes(…)
selectSingleNode() then selectNodes()
Document Object Model

Index