MuleSoft Technical Guides

DataWeave for reading XML

User MuleSoft Integration Team
Calendar May 14, 2021

The XML data structure is mapped to DataWeave objects containing other objects, strings, or null values.

All Mule applications have an XML file that specifies the resources that compose the application. Schemas define the configurable attributes of these resources that are referenced in the XML configuration file. This is how a Mule application both validates and illustrates its functional components and their configuration.

 

Sample XML data:

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

<Books:myns xmlns:Books="http://training.mulesoft.com/Books">
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
</Books:myns>

 

1. Converting XML to JSON:

JSON data format, values map one-to-one with DataWeave values.

DataWeave expression:

      %dw 2.0
   output application/json
   ---
   Payload

 

DataWeave Output:

    {
 "bookstore": {
   "book": {
     "title": "Everyday Italian",
     "author": "Giada De Laurentiis",
     "year": "2005",
     "price": "30.00"
   },
   "book": {
     "title": "Harry Potter",
     "author": "J K. Rowling",
     "year": "2005",
     "price": "29.99"
   },
   "book": {
     "title": "Learning XML",
     "author": "Erik T. Ray",
     "year": "2003",
     "price": "39.95"
   }
 }
}

 

2. Extract the Namespace:

DataWeave fully supports XML namespaces. It is a 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:

%dw 2.0
                   output application/json
                   ---
                   payload.myns.#

 

DataWeave Output:

                   "http://training.mulesoft.com/Books"

 

3. 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:

           %dw 2.0
   output application/json
   ---
payload.bookstore.*book

 

DataWeave output:

         [
 {
   "title": "Everyday Italian",
   "author": "Giada De Laurentiis",
   "year": "2005",
   "price": "30.00"
 },
 {
   "title": "Harry Potter",
   "author": "J K. Rowling",
   "year": "2005",
   "price": "29.99"
 },
 {
   "title": "Learning XML",
   "author": "Erik T. Ray",
   "year": "2003",
   "price": "39.95"
 }
]

 

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

DataWeave expression:

      %dw 2.0
   output application/json
   ---
   payload.bookstore.*book map
  {
    title: $["title"],
    author: $["author"],
    year: $.year,
    price:$.price
  }

 

DataWeave output:

     [
 {
   "title": "Everyday Italian",
   "author": "Giada De Laurentiis",
   "year": "2005",
   "price": "30.00"
 },
 {
   "title": "Harry Potter",
   "author": "J K. Rowling",
   "year": "2005",
   "price": "29.99"
 },
 {
   "title": "Learning XML",
   "author": "Erik T. Ray",
   "year": "2003",
   "price": "39.95"
 }
]

 

4. Transforming XML attributes:

DataWeave provides a mechanism that allows the reading of attributes from an XML element using the @ symbol followed by the key.

DataWeave expression:

       %dw 2.0
    output application/json
    ---
    payload.bookstore.*book map
  {
     title: $["title"],
     author: $["author"],
     year: $.year,
     price:$.price,
     category:$.@category
  }

 

DataWeave output:

                        [
    {
       "title": "Everyday Italian",
       "author": "Giada De Laurentiis",
       "year": "2005",
       "price": "30.00",
       "category": "COOKING"
    },
  {
      "title": "Harry Potter",
      "author": "J K. Rowling",
      "year": "2005",
      "price": "29.99",
      "category": "CHILDREN"
  },
 {
    "title": "Learning XML",
    "author": "Erik T. Ray",
    "year": "2003",
    "price": "39.95",
    "category": "WEB"
  }
 ]

 

Leave a comment

Your email address will not be published. Required fields are marked *