How to map with Serialize: XML

Use Case

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.

2 Likes