Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through a full XML document

I'm looking for a way to loop through all the nodes of my XML document.

XML file sample

<root>
    <llnode created="2005-05-24T15:26:24" createdby="42912153" createdbyname="" description="" id="107810306" modified="2008-06-05T16:07:44" name="" objname="" objtype="0" ownedby="42912153" ownedbyname="" parentid="107810295" size="4">
        <Nickname domain=""/>
        <MajorMinorContainer>false</MajorMinorContainer>
        <llnode created="2005-05-06T12:54:03" createdby="42912153" createdbyname="" description="" id="107815681" modified="2006-12-04T14:39:51" name="" objname="" objtype="0" ownedby="42912153" ownedbyname="" parentid="107810306" size="0">
            <Nickname domain=""/>
            <MajorMinorContainer>false</MajorMinorContainer>
        </llnode>
        <llnode created="2005-05-06T12:54:31" createdby="42912153" createdbyname="" description="" id="107815683" modified="2006-12-04T14:39:53" name="" objname="" objtype="0" ownedby="42912153" ownedbyname="" parentid="107810306" size="0">
            <Nickname domain=""/>
            <MajorMinorContainer>false</MajorMinorContainer>
        </llnode>
    </llnode>
    <llnode created="2005-05-24T15:26:24" createdby="42912153" createdbyname="" description="" id="107810306" modified="2008-06-05T16:07:44" name="" objname="" objtype="0" ownedby="42912153" ownedbyname="" parentid="107810295" size="4">
        <Nickname domain=""/>
        <MajorMinorContainer>false</MajorMinorContainer>
        <llnode created="2005-05-06T12:54:03" createdby="42912153" createdbyname="" description="" id="107815681" modified="2006-12-04T14:39:51" name="" objname="" objtype="0" ownedby="42912153" ownedbyname="" parentid="107810306" size="0">
            <Nickname domain=""/>
            <MajorMinorContainer>false</MajorMinorContainer>
        </llnode>
        <llnode created="2005-05-06T12:54:31" createdby="42912153" createdbyname="" description="" id="107815683" modified="2006-12-04T14:39:53" name="" objname="" objtype="0" ownedby="42912153" ownedbyname="" parentid="107810306" size="0">
            <Nickname domain=""/>
            <MajorMinorContainer>false</MajorMinorContainer>
        </llnode>
    </llnode>
</root>

The document always has the same structure. Each llnode represents a folder. This can go really deep (for the purpose of the above example, the scope is only 2, but it can go up to 10).

How can I loop through all the records? I don't want to put a loop into a loop and then another loop and do this like 20 times to be sure to handle every node. Is there a way to just do a loop of loops?

Below is what I got so far, only working for the actual XML document (scope=2), would need to add as much loops as the scope increases (it shouldn't go over scope=10)

Original VBA (from original question)

xmlExportDoc = "myXmlDoc.xml"

Set xmlDoc = New MSXML2.DOMDocument
xmlDoc.Load (xmlExportDoc)

Set xmlNodeList = xmlDoc.SelectNodes("//llnode")

For Each Node In xmlNodeList
   MsgBox "Listing the EXISTING nodes"
   MsgBox Node.nodeName & " " & Node.NodeValue & " " & Node.NodeType

   If Node.HasChildNodes() Then
      MsgBox Node.nodeName & "has child nodes"
      Set xmlNodeList2 = Node.ChildNodes

      For Each Node2 In oNodeList2
          MsgBox Node2.nodeName & " " & Node2.NodeValue & " " & Node2.NodeType

          If Node2.HasChildNodes() Then
             MsgBox Node2.nodeName & "has child nodes"
          End If
      Next
   End If
Next

UPDATED VBA

Private Function xmlParse(n As MSXML2.IXMLDOMNode)
    Dim n2 As MSXML2.IXMLDOMNode
    MsgBox n.nodeName & " " & n.NodeValue & " " & n.NodeType

    If n.HasChildNodes() Then
        MsgBox n.nodeName & " has child nodes"

        For Each n2 In n.ChildNodes
            xmlParse (n2)
        Next

        MsgBox "Done listing child nodes for " & n.nodeName
    End If
End Function

And the code of the event:

    Dim xmlExportDoc As String
    Dim xmlDoc As MSXML2.DOMDocument
    Dim xmlNodeList As MSXML2.IXMLDOMNodeList, xmlNodeList2
    Dim Node As MSXML2.IXMLDOMNode

    xmlExportDoc = "http://myserver.com/myDoc.xml"

    Set xmlDoc = New MSXML2.DOMDocument
    xmlDoc.async = False
    xmlDoc.Load (xmlExportDoc)

    Set xmlNodeList = xmlDoc.SelectNodes("//llnode")

    For Each Node In xmlNodeList
        Call xmlParse(Node)
    Next

This still don't work, got an error when doing the recursive xmlParse() call because MSXML2.IXMLDOMNode.ChildNodes doesn't seem to be a MSXML2.IXMLDOMNode type.

like image 257
dan Avatar asked Jan 18 '13 14:01

dan


People also ask

What is loop element in XML?

MS XML connectors The loop element allows you to define the iterating object. Generally the Loop element is also the row generator.

What is the difference between XDocument and XmlDocument?

XDocument is from the LINQ to XML API, and XmlDocument is the standard DOM-style API for XML. If you know DOM well, and don't want to learn LINQ to XML, go with XmlDocument . If you're new to both, check out this page that compares the two, and pick which one you like the looks of better.

Which of the following tag is used to loop over nodes in an XML document?

JSTL - XML <x:forEach> Tag The <x:forEach> tag is used to loop over nodes in an XML document.

How to loop through all the clients in an XML file?

We will have to loop through all the clients in the XML file. We achieve this by using the following expression in the collection: The expression works as follows: xpath () - Allows us to choose parts of an XML document. var:read_file - This is the variable we made to read the file.

How do I work with XML?

Working with XML can be done via a file or a webservice connection that returns XML. We'll use a file as an example in this article. Save the following XML code as an XML file to get started. Create a model with a file property and create a new record in which you upload the XML file.

How to iterate through XML in JavaScript?

Unfortunately, as a markup language, XML is quite unintuitive to iterate through. This mainly due to the text in each element being a node that needs to be traversed through. So, how can you loop through XML in JavaScript? Well, we can traverse into the text node of each element to extract the text data in the following order:

How do I read an XML file with multiple clients?

The example XML contains multiple clients and their pets. We will loop through this XML and save this data into the Client and Pet model. Create a new action for the model containing the XML file first. Then, to read the file, create a text expression variable. We'll use the following expression because our files property is XML:


1 Answers

I think what you need here is a recursive function. I don't really know VBA syntax so forgive the pseudocode, but you should be able to do something like this:

Set xmlNodeList = xmlDoc.SelectNodes("/*/llnode")
For Each node in xmlNodeList
    ListNodes(node)
Next

Function ListNodes(n As Node) 
     MsgBox n.nodeName & " " & n.NodeValue & " " & n.NodeType
     If n.HasChildNodes() Then
        MsgBox n.nodeName & "has child nodes"
        For Each n2 in n.ChildNodes
           ListNodes(n2)
        Next
        MsgBox "Done listing child nodes for " & n.nodeName
     End If   
End Function
like image 191
JLRishe Avatar answered Oct 11 '22 23:10

JLRishe