DataWeave for reading XML

Published on
May 14, 2021
Author
MuleSoft Integration Team
DataWeave for reading XML

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"
  }
 ]

Recent Blogs

Salesforce Pricing Automation: Boost Efficiency And Accuracy with Apex Triggers
BlogSep 9, 2025

Salesforce Pricing Automation: Boost Efficiency And Accuracy with Apex Triggers

Introduction In order to succeed in today’s fast-paced business landscape, precision and speed define competitive advantage. For businesses, especially those managing complex product catalogs, ensuring accurate pricing on sales orders or custom lines can be a time-consuming and error-prone task. To overcome this challenge, Salesforce trigger handlers offer a powerful solution to automate the entire… Continue reading Salesforce Pricing Automation: Boost Efficiency And Accuracy with Apex Triggers

Read More
Blog
6 min read

Salesforce Pricing Automation: Boost Efficiency And Accuracy with Apex Triggers

Introduction In order to succeed in today’s fast-paced business landscape, precision and speed define competitive advantage. For businesses, especially those managing complex product catalogs, ensuring accurate pricing on sales orders or custom lines can be a time-consuming and error-prone task. To overcome this challenge, Salesforce trigger handlers offer a powerful solution to automate the entire… Continue reading Salesforce Pricing Automation: Boost Efficiency And Accuracy with Apex Triggers

Read More
Connecting MuleSoft and Azure SQL with Entra ID
BlogJul 14, 2025

Connecting MuleSoft and Azure SQL with Entra ID

Introduction Establishing a secure connection between MuleSoft and Azure SQL Database can be challenging, especially if you are using Entra ID (formerly known as Azure Active Directory) for authentication. This blog walks through a fully working configuration for connecting to Azure SQL using ActiveDirectoryServicePrincipal in Mule runtime 4.7.4 with Java 8 — addressing driver setup,… Continue reading Connecting MuleSoft and Azure SQL with Entra ID

Read More
Blog
2 min read

Connecting MuleSoft and Azure SQL with Entra ID

Introduction Establishing a secure connection between MuleSoft and Azure SQL Database can be challenging, especially if you are using Entra ID (formerly known as Azure Active Directory) for authentication. This blog walks through a fully working configuration for connecting to Azure SQL using ActiveDirectoryServicePrincipal in Mule runtime 4.7.4 with Java 8 — addressing driver setup,… Continue reading Connecting MuleSoft and Azure SQL with Entra ID

Read More
Understanding Salesforce Flow Approval Processes
BlogJun 30, 2025

Understanding Salesforce Flow Approval Processes

Introduction: Salesforce introduced Flow Approval Processes in the Spring '25 release. This is an evolved version of the classic approval process model, powered by Flow Orchestrator. The new approach brings unprecedented flexibility, enabling the creation of dynamic, multi-level, and logic-driven approval workflows that are entirely declarative. Continue reading the blog to get a deeper understanding… Continue reading Understanding Salesforce Flow Approval Processes

Read More
Blog
5 min read

Understanding Salesforce Flow Approval Processes

Introduction: Salesforce introduced Flow Approval Processes in the Spring '25 release. This is an evolved version of the classic approval process model, powered by Flow Orchestrator. The new approach brings unprecedented flexibility, enabling the creation of dynamic, multi-level, and logic-driven approval workflows that are entirely declarative. Continue reading the blog to get a deeper understanding… Continue reading Understanding Salesforce Flow Approval Processes

Read More
Capturing Real-time Record Updation Using LWC
BlogMay 14, 2025

Capturing Real-time Record Updation Using LWC

Introduction In modern CRM ecosystems, real-time Salesforce integration and seamless user experiences are no longer optional but fundamental for driving operational efficiency. Imagine your sales reps making important Opportunity changes, but the ERP remains out of sync, leading to confusion and data errors. We understood the necessity to bridge this data gap and implemented a… Continue reading Capturing Real-time Record Updation Using LWC

Read More
Blog
5 min read

Capturing Real-time Record Updation Using LWC

Introduction In modern CRM ecosystems, real-time Salesforce integration and seamless user experiences are no longer optional but fundamental for driving operational efficiency. Imagine your sales reps making important Opportunity changes, but the ERP remains out of sync, leading to confusion and data errors. We understood the necessity to bridge this data gap and implemented a… Continue reading Capturing Real-time Record Updation Using LWC

Read More