我正在寻找一种方法来遍历我的 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
类型。