0

For some reason I can't find a simple answer to this question anywhere.

Given any domdocument e.g.

<root><div id="value" name="arga"><b>bold</b></div></root>

I want to simply loop through each node and print the node name, and list any attribute names and their values, if any. e.g. the above snippet would output:

root
div
   id = value
   name = arga
b

Pseudo-coded, it might look like this:

Function traverse(n)
    load the root node n, and print its name

    does the root node n have attributes? if yes, print them and their values

    if the root node n have child node(s) Then 

        for each childnode in child nodes
            traverse(childnode), 
        loop

    Else 'no child nodes 

        If sibling node(s) exist Then 
            for each sibling node in sibling nodes
                traverse(sibling node)
            Next
        End If

    End If

    traverse = results
End Function

Every example I can find involve selecting specific nodes, and specific named attributes.

Here is what I have so far, but it fails when used recursively.

Dim oDoc : Set oDoc = Server.CreateObject("MSXML2.DOMDocument.6.0")

If oDoc.LoadXML("<root d=""sdfgf""><div id=""asdfadfA"">slkdfhga</div><div id=""asdfadfA"">slkdfhga<b>this</b></div></root>") Then
echo traverse(oDoc.documentElement)
End If

Function traverse(n)

    Dim s

    s = s & typename(n) & "<br />"
    s = s & n.nodename & "<br />"
    s = s & n.nodetype & "<br />"

    'any attributes?
    Set oAttributes = n.attributes
    If isObject(oAttributes) Then 
        If oAttributes.length > 0 Then 
            'print attribs
            s = s & "---> has attributes <br />"
            For each oAttribute in oAttributes
                s = s & "--->" & oAttribute.Name & " = " & oAttribute.Value
            Next 
        End If
    End If

    'any children? 
    For Each oChildNode in n.childNodes
        'print child
        s = s & typename(oChildNode) & "<br />"
        s = s & oChildNode.nodename & "<br />"

        'recursive
        s = s & traverse(oChildNode)
    Next


    traverse = s.Value

End Function
GWR
  • 1,878
  • 4
  • 26
  • 44
  • You couldn't have looked very far, tons of examples how to do this. `documentElement` will give you the root element then use `.childNodes` to navigate down the structure. – user692942 Jan 01 '16 at 00:25
  • 2
    Possible duplicate of [Navigating XML nodes in VBScript, for a Dummy](http://stackoverflow.com/questions/9723903/navigating-xml-nodes-in-vbscript-for-a-dummy) – user692942 Jan 01 '16 at 00:26
  • Hmm, that question isn't the same. In that one, the code pulls specific named attributes. My question is looking for generic "loop through everything" Updated question with more detail – GWR Jan 01 '16 at 00:42
  • It shows you how to navigate. If the question is how do you list all the attributes that's different. Go look through the [IXMLDOMDocument](https://msdn.microsoft.com/en-us/library/ms757878(v=vs.85).aspx) it's all there. Use `.attributes` to navigate the attributes collection for each node. – user692942 Jan 01 '16 at 01:04
  • Another possible dup [Traversing all nodes in an XML file with VBScript](http://stackoverflow.com/questions/1908526/traversing-all-nodes-in-an-xml-file-with-vbscript) – user692942 Jan 01 '16 at 01:07
  • @Lankymart - I made a function with some pointers you gave. It throws an error when I try to use it recursively. Updated question. – GWR Jan 01 '16 at 01:57
  • Can you add the output to the question so we can see how far the code gets before the `traverse()` function fails? – user692942 Jan 01 '16 at 09:56
  • 2
    Its all worked out. Issue was with looping the nodes, you have to only check for children and attributes when the node type is not #text – GWR Jan 01 '16 at 17:32
  • 3
    Glad to hear you've sorted it, consider posting an answer there is nothing wrong with self answering on [so] in fact it's encouraged. – user692942 Jan 01 '16 at 20:50

0 Answers0