我正在读取一个
XML文件,其中包含一组看起来如下的测试结果:
<?xml version="1.0"?>
<testsuite>
<build>
<run>
<test>
<index>1</index>
<id>1</id>
<description>Description 1</description>
<result>Pass</result>
</test>
<test>
<index>2</index>
<id>2</id>
<description>Description 2</description>
<result>Aborted</result>
</test>
<test>
<index>3</index>
<id>3</id>
<description>Description 3</description>
<result>Dependency</result>
</test>
<test>
<index>4</index>
<id>4</id>
<description>Description 4</description>
<result>Failed</result>
</test>
</run>
</build>
</testsuite>
我可以通过使用以下方法成功获取列出的节点:
strQuery = "/testsuite/build/run/test/ (id|result)"
Set nodeslist = xmlDoc.selectNodes(strQuery)
而且我知道每个循环都使用a来获取节点值…
For Each objNode In nodeslist
'WHAT TO DO IN HERE...
Next
但是,我现在陷入了需要使用id及其相关结果的地步.本质上我将获取此信息并将结果上传到测试系统但是目前我仍然坚持如何遍历4个独立的测试节点并为每个测试节点选择id和结果,确保它们保持相互链接即,如果要将它们分配给ID和RESULT等变量,我可以在循环回来之前执行上传操作,然后将它们重新分配给下一个测试节点中的值.
任何帮助非常感谢.
最佳答案 正如你在评论中看到的那样
Dim sFSpec : sFSpec = resolvePath( "..\data\17049535.xml" )
' Dim sXPath : sXPath = "/testsuite/build/run/test/ (id|result)"
' msxml6.dll: NodeTest expected here. /testsuite/build/run/test/ -->(<--id|result)
Dim sXPath : sXPath = "/testsuite/build/run/test"
Dim oXDoc : Set oXDoc = CreateObject( "Msxml2.DOMDocument.6.0" )
oXDoc.setProperty "SelectionLanguage", "XPath"
oXDoc.async = False
oXDoc.load sFSpec
If 0 = oXDoc.ParseError Then
WScript.Echo sFSpec, "looks ok"
Dim ndlFnd : Set ndlFnd = oXDoc.selectNodes( sXPath )
If 0 = ndlFnd.length Then
WScript.Echo "|", sXPath, "| not found"
Else
WScript.Echo "found " & ndlFnd.length & " nodes."
Dim ndTest
For Each ndTest In ndlFnd
WScript.Echo ndTest.childNodes(0).tagName, ndTest.childNodes(0).text
WScript.Echo ndTest.childNodes(1).tagName, ndTest.childNodes(1).text
WScript.Echo ndTest.selectSingleNode("description").xml
Next
End If
Else
WScript.Echo oXDoc.ParseError.Reason
End If
我从你的XPath查询中得到了一个错误.输出:
E:\trials\SoTrials\answers\8194209\data\17049535.xml looks ok
found 4 nodes.
index 1
id 1
<description>Description 1</description>
index 2
id 2
<description>Description 2</description>
index 3
id 3
<description>Description 3</description>
index 4
id 4
<description>Description 4</description>
从我更正统的XPath应该给你一个提示如何通过数字/位置或’sub’XPath查询来处理你的测试节点及其子节点的列表.