DataWeave: Generating XML

In this blog post, I will show you how to generate XML output from a JSON data source while avoiding some of the most common pitfalls and explain how to use encoding, namespaces, fields, and attributes.

Sample JSON data

Throughout this article, I will use the following JSON data in the DataWeave transformation examples.

XML as an output

Let’s see how to convert a JSON document into an XML output. 

Given that the JSON document shown above is the input data to the DataWeave expression below, the XML output will be as shown.

DataWeave Expression:

Output Preview:

Note that the DataWeave expression specifies a root element family for the transformation to XML, I’ll explain more about why this is necessary later.

Encoding type

The output MIME type declaration can be followed by encoding to specify the encoding type for the XML output.

DataWeave Expression:

Output Preview:

By default, coding is set to UTF-8, however, any of the Java SE 8 supported coding types can be selected. You can find a list of valid encodings on Oracle Documentation site.

XML namespaces

DataWeave supports XML namespaces.

An XML namespace is a W3C recommended mechanism that avoids name conflicts by differentiating XML elements or attributes that may have identical names, but different definitions.

To enable an XML namespace:

  • Declare each namespace with the keyword ns followed by the DataWeave name of the namespace and the URL of the namespace.
  • Use the DataWeave name of the namespace before the element with # as the separator.

In the example below, the namespace simpsons is defined in the header of the DataWeave script and used in the transformation by prefixing the XML element with simpsons#.

DataWeave Expression:

Output Preview:

In the XML output, you can clearly see the simpsons namespace prefixing all elements.

Fields

  1. When transforming JSON to XML you may run into two difficulties: XML only allows one root element while JSON allows many. 
  2. There is no array concept in XML (only repeated elements), while the JSON data structure is based on arrays and maps.

To overcome these issues DataWeave provides the following solutions:

  1. To avoid multiple root issues, you must create a root element for the XML output.
  2. To transform a JSON array into XML repeated elements, use object constructor curly braces and evaluation parenthesis i.e. “{( )}“.

In the example below, the JSON data structure has multiple repeated roots and is structured as an array. The transformation to XML would fail if the above mentioned considerations were not taken into account. Thus the transformation script declares a root element as examples and wraps the JSON data in  “{( )}“.

DataWeave Expression:

Output Preview:

Let’s build on this example and transform the more complex JSON object provided at the beginning of this post. Its structure includes a collection of elements within an array.

To transform this input JSON as a collection of elements we have to iterate over the items using the map function.

DataWeave Expression:

Output Preview:

XML attributes

The XML specification allows attributes to be defined for elements but the JSON specification does not have a direct equivalent. DataWeave provides a mechanism that allows the insertion of attributes into an XML element using the “@” symbol followed by key-value pairs.

In the DataWeave script below, a function is called that determines if a Simpson is of majority and can therefore vote. The function is called for each element in the JSON array and added as an attribute to the member element.

DataWeave Expression:

Output Preview:

Conclusion

You now have the techniques necessary to successfully and perfectly transform JSON to XML using the DataWeave scripting language in your Mule applications.

If you want to learn more about DataWeave 2.0, you can register for the DataWeave course. You may be interested in my other DataWeave blog: DataWeave lambdas for Java programmers.



We'd love to hear your opinion on this post