循环遍历完整的XML文档

时间:2013-01-18 14:38:45

标签: xml vba ms-access recursion

我正在寻找一种循环遍历XML文档的所有节点的方法。

XML文件示例

<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>

文档始终具有相同的结构。每个 llnode 代表一个文件夹。这可能非常深(为了上面的例子,范围只有2,但最多可以达到10)。

如何循环浏览所有记录?我不想将循环放入循环然后再循环另外一个循环并执行20次以确保处理每个节点。有没有办法只做一个循环循环?

以下是我到目前为止所做的,仅适用于实际的XML文档(范围= 2),需要添加与范围增加一样多的循环(它不应超过范围= 10)

原始VBA (来自原始问题)

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

更新的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

事件的代码:

    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

这仍然不起作用,在执行递归xmlParse()调用时出错,因为MSXML2.IXMLDOMNode.ChildNodes似乎不是MSXML2.IXMLDOMNode类型。

2 个答案:

答案 0 :(得分:8)

我认为你需要的是一个递归函数。我真的不知道VBA语法,所以请原谅伪代码,但你应该可以这样做:

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

答案 1 :(得分:1)

这是我到目前为止所得到的:

xmlExportDoc = "http://www.mysite.com/myDoc.xml"

Dim xmldoc As MSXML2.DOMDocument
Dim xmlNode As MSXML2.IXMLDOMNode
Dim xmlNodeList As MSXML2.IXMLDOMNodeList
Dim myNode As MSXML2.IXMLDOMNode

Set xmldoc = New MSXML2.DOMDocument
xmldoc.async = False
xmldoc.Load (xmlExportDoc)
Set xmlNodeList = xmldoc.getElementsByTagName("*")
On Error Resume Next
For Each xmlNode In xmlNodeList
    For Each myNode In xmlNode.ChildNodes
        'Debug.Print xmlNode.Attributes(0).Text
    Next myNode
Next xmlNode
Set xmldoc = Nothing

如果层次结构不重要,它就可以工作,因为这个脚本只是循环遍历任何级别的节点。如果层次结构很重要,请查看JLRishe的答案。