vbscript获取XML节点值并分配给变量

时间:2013-06-11 16:48:33

标签: xml vbscript

我正在读取一个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等变量,我可以在循环回来之前执行上传操作并将它们重新分配给下一个测试节点中的值。

任何帮助都非常感激。

3 个答案:

答案 0 :(得分: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 :(得分: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

答案 2 :(得分:0)

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"