How to map with Serialize: XML

Table of contents

  • Use case 1
  • Use case 2

Use Case 1

Basically, Alumio uses the JSON data type in every transformer. While some systems sometimes only accept XML, you can convert the JSON data into XML by using the Value Mapper transformer and the Serialize: XML mapper.

Guide

1. Set attributes to an element
Please have a look at the below XML data.

<?xml version="1.0"?>
<data attr1="value 1" attr2="value 2">
    <content>this is content</content>
</data>

In order to get the above output, please have a look at the below JSON data. You should put an @ prefix to the JSON properties that you want to transform as attributes.

{
  "data": {
    "@attr1": "value 1",
    "@attr2": "value 2",
    "content": "this is content"
  }
}

2. Set the attribute to an element with value (no sub-elements)
Sometimes you want to add attributes to an element that has a value, such as below.

<?xml version="1.0"?>
<data attr="attr_value">
  <content title="this the title">this is content</content>
</data>

Please have a look at the content element, which has an attribute and a non-element value. In order to get the above output, below is the JSON data you need to provide.

{
  "data": {
    "@attr": "attr_value",
    "content": {
      "@title": "this the title",
      "#": "this is content"
    }
  }
}

A value of the # JSON property will be transformed as the content of its parent property in the XML output.

Use case 2

When trying to XML-serialize data, it sometimes has a string with special characters, like & that can be wrapped by cDATA in Alumio. If you run into such an issue, you can HTML-encode a string when XML-serializing and disable wrapping by cDATA of strings that contain & by making the following actions.

Disabling wrapping by cDATA strings with &

Here is a guide:

Step 1: Create a new transformer or navigate to the existing one, select “Data, transform data using mappers and conditions”, then select “Data transformers” and add the “Value setter” option.

Step 2: Input necessary information to the mandatory fields and add the “Serialize: XML” mapper from the list.

Step 3: The new “Disable cDATA wrapping” checkbox is displayed to avoid converting data from & will to &amp;

The “String: Encode HTML entities” mapper

This mapper converts characters to HTML entity encoded data, for example, & will transform to &amp;

Here is a guide:

Step 1: Create a new transformer, select “Data, transform data using mappers and conditions”, then select “Data transformers” and add the “Value setter” option.

Step 2: Input the Key and fill out a string that contains & {input}.

Step 3: Select the “String: Encode HTML entities” mapper from the list of mappers.

Step 4: Run the test. Output data will display data with &amp;

2 Likes