我正在读取一个 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>
Run Code Online (Sandbox Code Playgroud)
我可以使用以下命令成功获取列出的节点:
strQuery = "/testsuite/build/run/test/ (id|result)"
Set nodeslist = xmlDoc.selectNodes(strQuery)
Run Code Online (Sandbox Code Playgroud)
我知道使用 for every 循环来获取节点值......
For Each objNode In nodeslist
'WHAT TO DO IN HERE...
Next
Run Code Online (Sandbox Code Playgroud)
但是,我现在陷入了需要使用 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
Run Code Online (Sandbox Code Playgroud)
我从您的 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>
Run Code Online (Sandbox Code Playgroud)
我的更正统的 XPath 应该会提示您如何通过数字/位置或“子”XPath 查询来处理测试节点及其子节点的列表。