how to use data weave
TRANSCRIPT
How to use DataWeave component
14-05-2015
Abstract
• The main motto of this PPT is How to use DataWeave in our applications.
Introduction
• The DataWeave language is a simple, powerful tool to query and transform data inside of Mule.
Example
.mflow• <?xml version="1.0" encoding="UTF-8"?>• <mule xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw"
xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"• xmlns:spring="http://www.springframework.org/schema/beans" • xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"• xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd• http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd• http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd• http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">• <flow name="dataweaveFlow">• <file:inbound-endpoint path="D:\csvcontact" responseTimeout="10000" doc:name="File"/>• <logger message="flow started" level="INFO" doc:name="Logger"/>• <dw:transform-message metadata:id="5e76ab87-23c8-4c36-9618-6620c53520f8" doc:name="Transform Message">• <dw:set-payload><![CDATA[%dw 1.0• %output application/json• ---• {• • • Address: {• • • • Country: payload.Address.Country,• • • State: payload.Address.State• ,• • City: payload.Address.City• }• }]]></dw:set-payload>• </dw:transform-message>• <logger message="--Output--#[message.payloadAs(java.lang.String)]--" level="INFO" doc:name="Logger"/>• </flow>• </mule>
• Output:• INFO 2016-05-06 12:38:07,038 [[dataweave].connector.file.mule.default.receiver.01]
org.mule.transport.file.FileMessageReceiver: Lock obtained on file: D:\csvcontact\sample.xml
• INFO 2016-05-06 12:38:07,038 [[dataweave].dataweaveFlow.stage1.05] org.mule.api.processor.LoggerMessageProcessor: flow started
• INFO 2016-05-06 12:38:07,038 [[dataweave].dataweaveFlow.stage1.05] org.mule.api.processor.LoggerMessageProcessor: --Output--{
• "Address": {• "Country": "India",• "State": "AP",• "City": "Dharmavaram"• }• }--• • •
• Flow of execution:1. The above flow is used to convert xml to JSON
using DataWeave2. It will pick the .xml file from specific location and
will convert and display json response in console3. Sample xml:<Address><Country>India</Country><State>AP</State><City>Dharmavaram</City></Address>
References
• https://docs.mulesoft.com/mule-user-guide/v/3.7/dataweave