After reading this article you will know:

  • How to create XML using our Liquid Templating Language.

XML can be used to transfer data between applications.

Building XML

You can create XML in the following objects:

  • Text expression (Multi line) property
  • Text expression variable
  • Endpoint template
  • Soap template

You'll have to set quotes ('') around the XML when building it in an expression.

You can start building your XML structure from scratch, or start of with an example. Our example XML is build like this, presenting a breakfast menu:

<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
    <food>
        <name>Belgian Waffles</name>
        <price>$5.95</price>
        <description>
        Two of our famous Belgian Waffles with plenty of real maple syrup
        </description>
        <calories>650</calories>
    </food>
    <food>
        <name>Eggs on toast</name>
        <price>$4.95</price>
        <description>
        Eggs served on toast with cheese and ham.
        </description>
        <calories>480</calories>
    </food>
</breakfast_menu>

To insert our records in this template, we'll need to use the Liquid Templating Language. By combining both output ({{ }}) and tags ({% %}), we can generate the data from our records in the template where we want to use it. Read up on Liquid in this article: Liquid Templating Language Reference.

The data in the template is indicated by elements. These elements are given a name and start with an opening tag, followed by the value. The element is closed with a closing tag. Between the opening and closing tags, where using Liquid to dynamically generate a record's value. Example: 

<name>{{food.name}}</name>

This explains how to include a single record's value. Seeing multiple elements with the same name indicates multiple records. We'll use a Liquid For Loop to iterate through our records. First, create a collection of the data you want to use for the loop. Then use the collection in a liquid loop. In this example we use a collection named breakfast_food. Example:

{% for food in breakfast_food %}
 {{food.value}}
{% endfor %}

Within the loop, include the values of the iteration to insert in the elements. Your template should look something like this:

<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
    {% for food in breakfast_food %}
    <food>
        <name>{{food.name}}</name>
        <price>{{food.price}}</price>
        <description>
        {{food.description}}
        </description>
        <calories>{{food.calories}}</calories>
    </food>
    {% endfor %}
</breakfast_menu>

Save as .xml file

We can also save the generated data as an XML file. To do this we will have to create an endpoint on which the XML file will be created. Set the endpoint Content-Type to XML. Treat the path as a file, so put .xml behind it.

/breakfast_menu.xml

You can add an input variable key to the endpoint to protect it from outsiders. Create a text input variable. Generate a hard to guess key. Use liquid in your template to check if the given key is equal to your key like this:

{% if var:key == MY_GENERATED_KEY %} XML {% endif %}

Define the key in the URL to access the file/endpoint like this:

yourapp.bettywebblocks.com/breakfast_menu.xml?key=MY_GENERATED_KEY

Your endpoint will return an empty file if the key is not correctly defined.

Did this answer your question?