How to use DataWeave to read XML

At some point while developing a Mule application, it’s almost inevitable that you’ll need to manipulate XML. 

In this article, I will teach you how to read XML using DataWeave scripts and how to modify its encoding, namespaces, fields, and attributes with the goal to generate a JSON document output.

Getting started

To get started, we need some sample XML data as input to the DataWeave transformation we will write.

Sample XML data

Throughout this article we will use the following XML data snippet:

Extracting XML metadata

Let’s start by extracting metadata from the XML sample: the encoding type and the namespace.

Extract the encoding type

To extract the encoding type from the XML, unfortunately there is no elegant solution. What is required is a DataWeave expression to convert the payload using raw parsing and then to search it with a regular expression.

DataWeave Expression:

DateWeave output:

Extract the aNamespace

DataWeave fully 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 extract the namespace from an XML element use the hash symbol “#.”

DataWeave Expression:

DataWeave output:

Transforming XML fields

When transforming XML to JSON you may run into a couple of difficulties: 

  1. XML only allows one root element, while JSON allows multiple. 
  2. There is no array concept in XML (only repeated elements), however the JSON data structure is based on arrays and objects.

To overcome these issues, DataWeave provides the following solutions:

  1. To transform XML repeated elements into a JSON array, use the asterisk “*” DataWeave selector to create it.
  2. As JSON could have multiple root elements, you don’t have to do anything specific to generate the output.

DataWeave expression:

DataWeave output:

To specify your output format, you need to iterate over the items in your Array. That can be achieved with the map function.

Be careful, some fields contain dash symbol “-,” this means you cannot access the fields with the dot notation. Use the array notation $[“field-name”].

DataWeave expression:

DataWeave output:

Transforming 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 reading of attributes from an XML element using the @ symbol followed by the key.

The DataWeave script below extracts attribute information from the XML sample and converts it into JSON.

DataWeave expression:

DataWeave output:

Conclusion

In the previous article, you saw how to generate XML from JSON, now you are able to generate JSON from XML.

If you want to learn more about DataWeave 2.0, you can register for the DataWeave course



We'd love to hear your opinion on this post


One Response to “How to use DataWeave to read XML”

  1. Hi Seb ,

    It would be great for us if you add multiref xml transformation example as well or publish a new post