VBScript提取XML节点值并赋值给变量

5
我正在读取一个包含测试结果的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)

而且我知道使用 for each 循环来获取节点的值...
For Each objNode In nodeslist

'WHAT TO DO IN HERE...

Next

然而,我现在卡在需要使用id及其相关结果的阶段。基本上,我将获取此信息并将结果上传到测试系统,但目前我无法循环遍历4个单独的测试节点并挑选出每个节点的id和结果,确保它们彼此链接,即使它们被分配给变量(例如ID和RESULT),我也可以在执行上传操作之前对它们进行操作,然后循环回来并重新将它们分配给下一个测试节点中的值。任何帮助都会非常感激。
3个回答

1

从注释中可以看出

  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可以给你一个提示,如何通过数字/位置或“子”XPath查询来处理测试节点及其子节点列表。


1
Set xmlDoc = CreateObject("MSXML.DomDocument")
xmlDoc.Load "testsuite.xml" 

For Each testNode In xmlDoc.selectNodes("/testsuite/build/run/test")

    id = testNode.SelectSingleNode("id").Text
    Wscript.Echo "test/id = " & id

    '// Process something //'

    result = "result"
    testNode.SelectSingleNode("result").Text = result

Next
xmlDoc.Save "testsuite.xml"

1
您的XPath表达式应该会引发错误,正如Ekkehard.Horner所指出的。不过,以下类似的表达式可能会起作用:
...
strQuery = "/testsuite/build/run/test/*[name()='id' or name()='result']"
Set nodeslist = xmlDoc.selectNodes(strQuery)
For i = 0 To nodeslist.Length - 1 Step 2
  WScript.Echo nodeslist(i).text & vbTab & nodeslist(i+1).text
Next

+0.49 - 你不能对节点集合使用 UBound() 函数;你需要从 nodeslist.length 开始。 - Ekkehard.Horner
真糟糕,我总是犯那个错误。谢谢你指出来了。现在已经修复了。 - Ansgar Wiechers

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接