Skip to content

How do I retrieve the node list from the given XML document using "index" and "name"

This example demonstrates how to retrieve the node list from a given XML document using the following methods within the context of an automated test in execution.
  • index
  • name
In this example, the XML data from which you have to retrieve the node list is stored and assigned a variable using the "store" command. Now you can get the node list in two ways, one is with an index by using the "xmlNodeListByIndex" command and another one is with node name by using the "xmlNodeListByPath" command. The node value will be stored in the variable given in the value field.

Solution:

Approach 1:

# Command Target Value
Store the XML in a variable
1 store <?xml version="1.0" encoding="UTF-8"?>
<insuranceData>
  <insurance category="valid">
    <age>45</age>
    <income>55000</income>
    <zipcode>90001</zipcode>
    <result>You are eligible for a subsidy.</result>
  </insurance>
  <insurance category="valid">
    <age>64</age>
    <income>25000</income>
    <zipcode>14301</zipcode>
    <result>You are eligible for a subsidy.</result>
  </insurance>
  <insurance category="invalid">
    <age>15</age>
    <income>25000</income>
    <zipcode>08857</zipcode>
    <result>Combinations of inputs provided out of scope for the calculator.</result>
  </insurance>
</insuranceData>
insuranceXmlData
To get the node list by using index value
2 xmlNodeListByIndex ${insuranceXmlData} || nodeValue (Ex: 1,2,3...,n) nodesList

Approach 2:

# Command Target Value
To store the XML data into a variable
1 store <?xml version="1.0" encoding="UTF-8"?>
<insuranceData>
  <insurance category="valid">
    <age>45</age>
    <income>55000</income>
    <zipcode>90001</zipcode>
    <result>You are eligible for a subsidy.</result>
  </insurance>
  <insurance category="valid">
    <age>64</age>
    <income>25000</income>
    <zipcode>14301</zipcode>
    <result>You are eligible for a subsidy.</result>
  </insurance>
  <insurance category="invalid">
    <age>15</age>
    <income>25000</income>
    <zipcode>08857</zipcode>
    <result>Combinations of inputs provided out of scope for the calculator.</result>
  </insurance>
</insuranceData>
insuranceXmlData
To get the node list by using index value
2 xmlNodeListByPath ${insuranceXmlData} || /insurance nodesList

Tips, Tricks, Gotchas & Best Practices:  

  • The XML should be valid and encoded in "UTF-8" format.
  • You can use the "xmlAssertPathExists" command to check if the path is valid or invalid.
  • The nodeValue should not be greater than existing nodes.
  • The "xmlNodeListByPath" command can be used to get child node values also. (Ex: /insurance/age)


Feedback and Knowledge Base