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:
- XML only allows one root element, while JSON allows multiple.
- 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:
- To transform XML repeated elements into a JSON array, use the asterisk “*” DataWeave selector to create it.
- 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

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

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

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

All About Schedulers: Mule 4
In the world of Mule 4, automating repetitive tasks and triggering flows at defined intervals is necessary for building efficient and robust integration solutions. This is where Mule 4 schedulers come into use. This blog post explores the intricacies of scheduling in Mule 4, providing practical examples and best practices to help you get deeper… Continue reading All About Schedulers: Mule 4
All About Schedulers: Mule 4
In the world of Mule 4, automating repetitive tasks and triggering flows at defined intervals is necessary for building efficient and robust integration solutions. This is where Mule 4 schedulers come into use. This blog post explores the intricacies of scheduling in Mule 4, providing practical examples and best practices to help you get deeper… Continue reading All About Schedulers: Mule 4