# How to map with Serialize: XML

**URL:** <https://forum.alumio.com/t/how-to-map-with-serialize-xml/356>\
**Category:** Transformers\
**Tags:** mappers\
**Created:** [January 10, 2023, 9:28am UTC](https://forum.alumio.com/t/how-to-map-with-serialize-xml/356 "2023-01-10T09:28:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gugi](https://forum.alumio.com/user_avatar/forum.alumio.com/gugi/32/236_2.png) [@Gugi](https://forum.alumio.com/u/Gugi)\
**Post date:** [January 10, 2023, 9:28am UTC](https://forum.alumio.com/t/how-to-map-with-serialize-xml/356/1 "2023-01-10T09:28:37Z")

</div>

### 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.

 ![DjV90-mA-ruNhDjYGmxIRO6neMbd62E1GQ](https://alumio-forum.s3.dualstack.eu-central-1.amazonaws.com/uploads/original/1X/3e83b97836939a57207bc27a1a758f12e46955f1.png)

# Guide

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

```auto
<?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.

```auto
{
  "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.

```auto
<?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.

```auto
{
  "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.

**3. Create an array of elements**

Let’s say you require Alumio to create the following XML.

```xml
<?xml version="1.0"?>
<Products>
    <Product>
        <SKU>SKU0001</SKU>
        <Name>Product ABC</Name>
    </Product>
    <Product>
        <SKU>SKU002</SKU>
        <Name>Product XYZ</Name>
    </Product>
</Products>

```

In the above XML format, you can see multiple “Product” elements inside “Products”. In that case, you should put the array of the product objects inside the property "Products” → “Product”, as shown below.

```json
{
  "Products": {
    "Product": [
      {
        "SKU": "SKU001",
        "Name": "Product ABC"
      }
      {
        "SKU": "SKU002",
        "Name": "Product XYZ"
      }
    ]
  }
}

```

# 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;`

 ![image](https://alumio-forum.s3.dualstack.eu-central-1.amazonaws.com/uploads/original/2X/3/30cbdfb45bf24a1451d99efbaafb2a0957e7a178.png)

### 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;`

 ![image](https://alumio-forum.s3.dualstack.eu-central-1.amazonaws.com/uploads/original/2X/f/f9fd590e2616bc79e400282af842633eb0a43866.jpeg)
