Vbscript takes XML node values ​​and assigns variables

I am reading in an XML file that contains a test result set that looks like this:

<?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 successfully get the list of nodes using the following:

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

And I know that for each cycle, use node values ​​for each cycle ...

 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 identifier and the result associated with it. In fact, I take this information and load the result into the test system, but for now I am fixated on how to scroll through 4 separate test nodes and select an identifier and result for each of them, ensuring that they remain connected to each other, etc. e. if they should be assigned to variables like ID and RESULT, which then I could perform my load action before you go back and reassign them to the values ​​in the next node test.

Any help is greatly appreciated.

+4
source share
3 answers

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 an XPath request. 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, I should give you a hint on how to handle the list of your test nodes and their children by number / position or by the query β€œsub” XPath.

+1
source

Your XPath expression should throw an error, as pointed out by Ekkehard.Horner . Perhaps something like this might work:

 ... 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 
+1
source
 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" 
0
source

Source: https://habr.com/ru/post/1485652/


All Articles