2013-06-11 21 views
4

我在包含組測試結果(如下所示)的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) 

我知道使用for each循環搶節點值...

For Each objNode In nodeslist 

'WHAT TO DO IN HERE... 

Next 

如何曾經,我現在停留在需要使用id及其相關結果的地步。本質上,我將採取這些信息並將結果上傳到測試系統,但目前我被困在如何循環訪問4個獨立的測試節點併爲每個測試節點挑選ID和結果,確保它們保持相互關聯即如果將它們分配給ID和RESULT等變量,那麼我可以在循環回去之前執行我的上載操作,並將它們重新分配給下一個測試節點中的值。

任何幫助非常感謝。

回答

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查詢自己的孩子的名單。

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" 
1

Ekkehard.Horner指出的那樣,您的XPath表達式應該引發錯誤。這樣的東西可能工作,但:

... 
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

+0.49 - 你不能在節點集合上使用UBound();你必須從nodeslist.length –

+0

開始,我*總是*犯這個錯誤。感謝您的支持。現在已經修復了。 –

+0

+1花式XPath –