How to read an XML into a variable?

Learn how you can read and find values from XML files in ScenarioBuilder.

XML stands for EXtensible Markup Language. It is designed to store and transfer small to medium amounts of data and is widely used for sharing structured information.

ScenarioBuilder lets you read an XML and then parse the elements, their attributes, and values. In this article, we will see how we can read and save the values of an XML file in a variable array using "Read Text Into a Variable" action. 

Let's try to learn some basic XPath syntax by looking at some examples before moving forward.

We will use the following XML document called books.xml in the examples below. 

Screenshot_99-1

1. Select all the titles

/bookstore/book/title

2. Select the title of the first book

/bookstore/book[1]/title

3. Select price nodes with price>35

/bookstore/book[price>35]/price

4. Select title nodes with price>35

/bookstore/book[price>35]/title

 

*To learn more about XPath syntax, see this article.

Read XML Into a Variable

The "Read Text Into a Variable" action can be found under the "Control Actions" group in the “Actions” list located on the left side of ScenarioBuilder.

Properties

  • XML File Path – Select the XML file name and location.

The XML files should already exist on the machine where the execution will happen.

  • Output Variable Array – This is the name that will be used to refer to the output array. For example, XmlValues.
  • Read Variable(s) – The variables that are expected to be read from the XML file. If parsed correctly, the result will be an array with the Output Variable name and the index of this variable. For example, if the output variable name is 'XmlValues', and the first variable is parsed, then you can use %XmlValues[1]% to get the variable value.
    • Read Variable #1 – The index in the output variables array and the path to identify its value. The path is the XPath to the item e.g. /weather/forecast[temp>20]/temp.
  • Continue On Failure – If Checked the execution of the scenario will continue.

How to find a value from an XML file?

Example 1:

In this example, we will be using an XML file called note.xml, which contains basic information about a note.

Screenshot_92-2

Let's say we want to find the text of the body, save it into a variable and display it in a dialog box.

1. Add “Read XML Into a Variable“ action to your scenario in ScenarioBuilder.

2. Specify the XML file and its location. C:\Users\sbuser\Documents\XMLfiles\note.xml

3. Put a name for the variable array. i.e. body_note

4. Click "+" to add a read variable and then specify the elements in order. 

i.e. /note/body

5. Add "Dialog Box" action and type the variable along with the index. i.e. %body_note[1]%

Screenshot_94-1

Example 2:

In this example, we will be using an XML file called simple.xml, which contains a breakfast menu with various foods information. 

Screenshot_90-2

Let's say we want to find the price of the "French Toast", save it into a variable and display it in a dialog box.

1. Add “Read XML Into a Variable“ action to your scenario in ScenarioBuilder.

2. Specify the XML file and its location. ie. C:\Users\sbuser\Documents\XMLfiles\simple.xml

3. Put a name for the variable array. i.e. foodPrice

4. Click "+" to add a read variable and then specify the elements in order. 

i.e. /breakfast_menu/food[4]/price 

5. Add "Dialog Box" action and type the variable along with the index. i.e. %foodPrice[1]%

*The index is 4 since it is the fourth food element.

Screenshot_97-2