Warning!
This is a legacy document. The features described below are only usable within the classic-generation environment.
Currently, Betty Blocks offers faster and more advanced options that are available in next-gen. Before you start working on some new features in your application, consider doing it using the next-gen version. Good luck!
Working with XML can be done via a file or a webservice connection that returns XML. We'll use a file as an example in this article.
After reading this article you will know:
How to create a loop using XML data
Looping through XML
Save the following XML code as an XML file to get started. Create a model with a file property and create a new record in which you upload the XML file. Next up, create two more models, one called Client and one called Pet, give both models a Text (single line) property called Name, then proceed.
<?xml version="1.0" encoding="UTF-8"?>
<Info xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<Clients>
<Client>
<Name>James</Name>
<Pets>
<Pet>Dog</Pet>
<Pet>Cat</Pet>
</Pets>
</Client>
<Client>
<Name>Peter</Name>
<Pets>
<Pet>Fish</Pet>
</Pets>
</Client>
<Client>
<Name>Frank</Name>
<Pets>
<Pet>Bird</Pet>
<Pet>Turtle</Pet>
<Pet>Mouse</Pet>
</Pets>
</Client>
</Clients>
</Info>
The example XML contains multiple clients and their pets. We will loop through this XML and save this data into the Client and Pet model.
Create a new action for the model containing the XML file first. Then, to read the file, create a text expression variable. We'll use the following expression because our files property is XML:
read_file(var:record.xml)
Next, we'll make a collection to loop through. We will have to loop through all the clients in the XML file. We achieve this by using the following expression in the collection:
xpath(var:read_file, '/Info/Clients/Client', true)
The expression works as follows:
xpath() - Allows us to choose parts of an XML document.
var:read_file - This is the variable we made to read the file.
'/Info/Clients/Client' - This is the XML part we need (all the clients).
true - Indicates that the variable should ignore any other XML attributes (such as xmlns="value").
Steps
Create a loop step in which we loop through the created collection. Don't forget to set the index variable name, as we are going to need this later.
Add a create step in the loop, the model for this step is Client. Add the following text expression variable to get the name of the client:
xpath(var:read_file, '/Info/Clients/Client[' + var:index + ']/Name/text()', true)
[index] - Selects the client using the index number, this makes sure every loop uses the next client from the XML.
/text() - This removes the XML tags from the value. <Name>John</Name> --> John.
Assign the variable to the name property of the Client model.
Now, add another loop in the loop to get the pets, once again, don't forget to set the index variable, name it petindex. Create a collection for the pets using the following expression:
xpath(var:read_file, '/Info/Clients/Client[' + var:index + ']/Pets/Pet', true)
Next, add a create step in the new loop. In this step we will get the pet name and save it to the name property within the Pet model. First, create a text expression using the following expression:
xpath(var:read_file, '/Info/Clients/Client[' + var:index + ']/Pets/Pet[' + var:petindex + ']/text()', true)
Assign the variable to the name property. Your action is now finished and should create both clients and pets when you run it!