5

I am reading in an XML file that contains a set of test results that looks as follows:

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

I can succesfully get the nodes listed by using the following:

 strQuery = "/testsuite/build/run/test/ (id|result)"
 Set nodeslist = xmlDoc.selectNodes(strQuery)

And I know to use a for each loop to grab the node values...

For Each objNode In nodeslist

'WHAT TO DO IN HERE...

Next

However, I am now stuck at the point where I need to use the id and its associated result. Essentially I will take this information and upload the result to a test system but at the moment I am stuck on how to loop through the 4 separate test nodes and pick out the id and result for each one, ensuring that they remain linked to each other i.e. if they were to be assigned to variables such as ID and RESULT which I could then perform my upload action on before looping back around and re-assigning them to the values within the next test node.

Any help much appreciated.

3 Answers 3

1

As you can see from the comment in

  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

I got an error from you XPath query. The output:

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>

from my more orthodox XPath should give you a hint how to deal with a list of your test nodes and their children by either number/position or a 'sub' XPath query.

Sign up to request clarification or add additional context in comments.

Comments

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"

Comments

1

Your XPath expression should raise an error, as Ekkehard.Horner pointed out. Something like this might work, though:

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

+0.49 - you can't use UBound() on a node collection; you'll have to start with nodeslist.length
Darn, I always make that mistake. Thanks for catching. It's fixed now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.