How do I insert data in the XML document at the given path or value
This example demonstrates how to insert data in the XML document at the given path or value within the context of an automated test in execution.
In this example, the XML data in which you have to insert values is stored and assigned a variable using the "store" command. Now by using the "xmlInsertIntoDocument" command, you can insert an XML value to the given path. The modified XML data is stored in the variable given in the value field.
Solution:
# | 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 Insert data in the XML document in the given path | |||
2 | xmlInsertIntoDocument | ${insuranceXmlData}||{/insuranceData//insurance[1]/test,<test1>test1</test1>} |
insuranceXmlData |
Tips, Tricks, Gotchas & Best Practices:
- The XML should be valid and encoded in "UTF-8" format.
- The new node value should be given in the XML path to insert the value. (Ex: /insuranceData//insurance[1]/test - Here "test" is the new node).
- You can insert multiple values in the given path by using the || symbol as mentioned in the "xmlInsertIntoDocument" command article.
- You can use the "xmlAssertPathExists" command to check if the path is valid or invalid.