DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Data Pipeline Techniques in Action
  • Tobiko Data: Revolutionizing Data Transformation With SQLMesh
  • Machine Learning With Python: Data Preprocessing Techniques
  • Financial Data Engineering in SAS

Trending

  • Chaos Engineering and Kafka In Action: Topic Level Idempotence and Retry Check
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Your Event Storming Is Not Your Software Design
  • Data Integration Landscape 2026: Event Streaming, API, and Batch in the Era of Agentic AI

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Data
  4. Data Transformation in Mule Using DataWeave

Data Transformation in Mule Using DataWeave

Data transformation is critical in software development and digitalization. DataWeave can be used in data transformation to convert CSV files to XML.

By 
Rajeshkumar D
·
Jan. 23, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
25.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Data transformation is essential to day-to-day activities within an organization. It plays a critical role in digitalization.

DataWeave is the component provided by Mule for data transformation. In this article, I'm going to explain how to use it as part of Anypoint Studio, which is a GUI for building Mule flows from MuleSoft.

As part of the example, we will be converting the CSV file to an XML file using the DataWeave component. The DataWeave component is part of Anypoint Studio, so there is no need for separate installation.

MULE ESB 3.8 and Anypoint Studio 6.0 are used in this tutorial.

In this case, the input is a CSV file. Consider the below sample data:

ID Name Salary
1 Rajesh 25,000
2 Rohan 25,000
3 Mike 28,000
4 Charan 1,000
5 Ravi

30,000

The expected XML output from this CSV file is:

<?xml version='1.0' encoding='windows-1252'?>
<Root>
  <employees>
    <employee>
      <id> 1</id>
      <ContactName>rajesh</ContactName>
      <sal>25000</sal>
    </employee>
 <employee>
      <id> 2</id>
      <ContactName>rohan</ContactName>
      <sal>25000</sal>
    </employee>
 <employee>
      <id> 3</id>
      <ContactName>mike</ContactName>
      <sal>28000</sal>
    </employee>
 <employee>
      <id> 4</id>
      <ContactName>charan</ContactName>
      <sal>1000</sal>
    </employee>
 <employee>
      <id> 5</id>
      <ContactName>ravi</ContactName>
      <sal>30000</sal>
    </employee>

  </employees>
</Root>

First, use the file inbound connector to read the file:

<file:inbound-endpoint path="src/main/resources/input"
moveToDirectory="src/main/resources/processed" responseTimeout="10000"
metadata:id="3bfcea9d-6035-4f91-adbf-8eaaedeb1179" doc:name="File" />

Second, drag and drop the transformer from the design pallet to the flow:

<dw:transform-message metadata:id="710ab9d5-1327-4b95-baf0-275ec9d29e63" doc:name="Transform Message">

        </dw:transform-message>

Next, double click on the transformer component and set the metadata for input and output.

  • Input: Give the sample CSV file.

  • Output: Give the sample XML file.

For example:

<dw:transform-message metadata:id="710ab9d5-1327-4b95-baf0-275ec9d29e63" doc:name="Transform Message">
            <dw:input-payload doc:sample="sample_data\list_csv.csv" mimeType="application/csv"/>
            <dw:set-payload><![CDATA[%dw 1.0
%output application/xml
---
{
Root: {
employees: {
(payload map ((payload01 , indexOfPayload01) -> {
employee: {
id: payload01." 1",
Name: payload01.paul,
sal: payload01."25000",

}
}))
}
}
}]]></dw:set-payload>
        </dw:transform-message>

Now, use the file outbound connector to store the XML file.

<file:outbound-endpoint path="src/main/resources/output"
responseTimeout="10000" doc:name="File" outputPattern="#[function:systime].xml"/>

Here, I have given the XML name as time stamp '#[function:systime].xml'.

Execute the Mule application and check the console for the successful deployment.

Give the sample CSV in the input folder. Mule will pick up and create the required XML in the output folder.

Here's the total flow for reference:

<flow name="csv-to-xmlFlow">
    <file:inbound-endpoint path="src/main/resources/input"
moveToDirectory="src/main/resources/processed" responseTimeout="10000"
metadata:id="3bfcea9d-6035-4f91-adbf-8eaaedeb1179" doc:name="File" />
        <dw:transform-message metadata:id="710ab9d5-1327-4b95-baf0-275ec9d29e63" doc:name="Transform Message">
            <dw:input-payload doc:sample="sample_data\list_csv.csv" mimeType="application/csv"/>
            <dw:set-payload><![CDATA[%dw 1.0
%output application/xml
---
{
Root: {
employees: {
(payload map ((payload01 , indexOfPayload01) -> {
employee: {
id: payload01." 01",
name: payload01.paul,
sal: payload01."25000",

}
}))
}
}
}]]></dw:set-payload>
        </dw:transform-message>

<logger level="INFO" metadata:id="41c24045-cac4-470e-9778-d347a736494c"
doc:name="Logger" message="#[payload]"/>
<file:outbound-endpoint path="src/main/resources/output"
responseTimeout="10000" doc:name="File" outputPattern="#[function:systime].xml"/>
</flow>

You're done!

Data transformation Data (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Data Pipeline Techniques in Action
  • Tobiko Data: Revolutionizing Data Transformation With SQLMesh
  • Machine Learning With Python: Data Preprocessing Techniques
  • Financial Data Engineering in SAS

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook