Dataweave, Development

Transforming XML Data with DataWeave: A Guide to Iteration

2 min read
avatar
Aravind Kumar Kumarappa

Iterating over XML in DataWeave is similar to iterating over arrays or lists. You can use the map or flatMap functions to iterate over nodes in the XML and transform them as needed.

Let’s start by considering the following XML:

  
   <employees>
    <employee>
        <id>101</id>
        <name>John Doe</name>
        <department>IT</department>
    </employee>
    <employee>
        <id>102</id>
        <name>Jane Smith</name>
        <department>HR</department>
    </employee>
    <employee>
        <id>103</id>
        <name>Bob Johnson</name>
        <department>Finance</department>
    </employee>
</employees>

Suppose we want to transform this XML to a JSON array of objects, where each object represents an employee and contains the id, name, and department fields. We can use the following DataWeave code to achieve this:
%dw 2.0
output application/json
---
payload.employees.*employee map {
  id: $.id,
  name: $.name,
  department: $.department
}
In this code, we use the map function to iterate over each employee node in the XML. We use the *employee selector to match all employee nodes under the employees node. Inside the map function, we create a new object with the id, name, and department fields by referencing the corresponding child nodes using the $ syntax.
The output of this code is a JSON array of objects:
[
  {
    "id": "101",
    "name": "John Doe",
    "department": "IT"
  },
  {
    "id": "102",
    "name": "Jane Smith",
    "department": "HR"
  },
  {
    "id": "103",
    "name": "Bob Johnson",
    "department": "Finance"
  }
]

We can also use the flatMap function to transform the XML in a different way. Suppose we want to transform the XML to a CSV string where each row represents an employee and contains the id, name, and department fields separated by commas. We can use the following DataWeave code to achieve this:

%dw 2.0
output text/csv
header=true
---
payload.employees.*employee flatMap ((employee, index) -> [
  (index == 0) ? ["id", "name", "department"] : null,
  [employee.id, employee.name, employee.department]
])

In this code, we use the flatMap function to iterate over each employee node in the XML. We use the $ syntax to reference the child nodes of the employee node. Inside the flatMap function, we create an array for each row of the CSV, with the first row containing the header and the subsequent rows containing the employee data. We use the ternary operator (index == 0) ? […] : null to conditionally include the header row.

The output of this code is a string in CSV format:

id,name,department
101,John Doe,IT
102,Jane Smith,HR
103,Bob Johnson,Finance

avatar
Aravind Kumar Kumarappa

Leave a Reply

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