我有一个示例XML文件,其构造如下:
<?xml version="1.0" encoding="UTF-8"?>
<ParentLevel1>
<ParentLevel2>
<ParentLevel3>
<ParentLevel4>
<MyNode>Data 1</MyNode>
<MyNode>Data 2</MyNode>
<MyNode>Data 3</MyNode>
<MyNode>Data 4</MyNode>
<MyNode>Data 5</MyNode>
<MyNode>Data 6</MyNode>
</ParentLevel4>
<ParentLevel4additional>My Data Matches</ParentLevel4additional>
</ParentLevel3>
<ParentLevel3>
<ParentLevel4>
<MyNode>Data 7</MyNode>
<MyNode>Data 8</MyNode>
<MyNode>Data 9</MyNode>
<MyNode>Data 10</MyNode>
<MyNode>Data 11</MyNode>
<MyNode>Data 12</MyNode>
</ParentLevel4>
<ParentLevel4additional>My Data does not Match</ParentLevel4additional>
</ParentLevel3>
</ParentLevel2>
</ParentLevel1>
如果<MyNode>
的值为“我的数据匹配”,我需要节点ParentLevel4
下的节点数ParentLevel4additional
。
我尝试使用以下脚本,但无法获得解决方案:
MyNodeCount = 0
AdditionNodeCount = 0
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
If (xmlDoc.Load(strXMLFile)) Then
Set AdditionNode =xmlDoc.selectNodes ("//ParentLevel1/ParentLevel2/ParentLevel3/ParentLevel4additional/")
For Each ParentLevel4additional in AdditionNode
if ParentLevel4additional.Text = "My Data Matches" Then
Set ObjMyNodes=xmlDoc.selectNodes ("//ParentLevel1/ParentLevel2/ParentLevel4/MyNode/")
For Each MyNode in ObjMyNodes
MyNodeCount = MyNodeCount + 1
Next
AdditionNodeCount = AdditionNodeCount + 1
End If
Next
Wscript.Echo MyNodeCount
Wscript.Echo AdditionNodeCount
Else
WScript.Echo "Error loading XML file '" & strXMLFile & "'." & vbCrLf & _
"Error code: 0x" & Hex(xmlDoc.ParseError.ErrorCode) & vbCrLf & _
"Description: " & xmlDoc.ParseError.Reason & vbCrLf & _
"Line: " & xmlDoc.ParseError.Line & vbCrLf & _
"Character: " & xmlDoc.ParseError.LinePos
WScript.Quit 1
End If
答案 0 :(得分:0)
您的代码中存在一些错误:
/
因为它会给你错误的结果。以下行中的XPath缺少级别(ParentLevel3
):
Set ObjMyNodes=xmlDoc.selectNodes ("//ParentLevel1/ParentLevel2/ParentLevel4/MyNode/")
即使您将缺失的级别添加到上面的行中,它仍然会计算所有MyNode
个节点,因为您从头开始重新选择节点。此外,如果您有多个匹配项,则会计算所有MyNode
个节点数乘以匹配数,这样您就会得到不需要的+重复结果。
您处于正确的轨道上,但您应该只选择匹配节点的兄弟节点。以下内容应该为您提供所需的结果:
If (xmlDoc.Load(strXMLFile)) Then
Set NodesList = xmlDoc.SelectNodes("//ParentLevel1/ParentLevel2/ParentLevel3")
For Each node in NodesList
Set ParentLevel4additional = node.SelectSingleNode("ParentLevel4additional")
If ParentLevel4additional.Text = "My Data Matches" Then
For Each myNode in node.SelectNodes("ParentLevel4/MyNode")
MyNodeCount = MyNodeCount + 1
Next
AdditionNodeCount = AdditionNodeCount + 1
End If
Next
Wscript.Echo MyNodeCount
Wscript.Echo AdditionNodeCount
Else
WScript.Echo "Error loading XML file '" & strXMLFile & "'." & vbCrLf & _
"Error code: 0x" & Hex(xmlDoc.ParseError.ErrorCode) & vbCrLf & _
"Description: " & xmlDoc.ParseError.Reason & vbCrLf & _
"Line: " & xmlDoc.ParseError.Line & vbCrLf & _
"Character: " & xmlDoc.ParseError.LinePos
WScript.Quit 1
End If
答案 1 :(得分:0)
使用xPath:
//ParentLevel3[ParentLevel4additional='My Data Matches']/ParentLevel4
您可以使用以下代码获取所需父节点的子节点数:
Dim xmlPath, objXml, xPath
xmlPath = "J:\Documents\Gurman\Work\PersonalWork\Misc Codes\26042018_SO\source.xml"
set objXml = CreateObject("Microsoft.Xmldom")
objXml.async = False
objXml.load xmlPath
xPath = "//ParentLevel3[ParentLevel4additional='My Data Matches']/ParentLevel4"
set objNode = objXml.selectSingleNode(xPath)
set childnodes = objNode.ChildNodes
i=0
for each node in childnodes
i = i+1
next
msgbox "No. of ChildNodes = " & i
set objNode = Nothing
set objXml = Nothing