0
votes

I want to convert DB records into XML. Select query from DB records is giving LinkedHashMap as payload. Now I want to use dataweave to transform this into XML. I am pretty new to mule so a bit lost. What all steps do I need to follow to do this?

Thank you.

3

3 Answers

0
votes

As informed by Victor please go through Dataweave Documentation which is the Data transformation component in Mule.

For you question the below methodology will help you to transform. The below script is an example that you can refer to be added to the data weave component in the mule code.

%dw 1.0
%output application/xml
---
payload map {
 element: $.columnName_Or_aliasName
}

Map function will iterate over an array or complex xml or array in JSON. Here for your DB response you can consider as every row in your DB resulset will be iterated

0
votes

Transform Java Map to XML using DataWeave :

You can try below POC :

<sub-flow name="testdataweaveSub_Flow16_MapToXml" doc:description="http://blogs.mulesoft.com/dev/getting-started-with-dataweave-part-1/ Literal Expressions Variable Reference Expressions">
        <set-payload value="{   &quot;item_id&quot;: &quot;B0002345W45&quot;,   &quot;item_type&quot;: &quot;Item Type 1&quot;,   &quot;item_type_name&quot;: &quot;Item Type 1 Name&quot;,   &quot;item_name&quot;: &quot;item 1 name&quot;,   &quot;item_summary&quot;: &quot;item 1 summary&quot;,   &quot;item_brand&quot;: &quot;Brand 1&quot;,   &quot;image_type_name&quot;: &quot;SmallImage&quot;,   &quot;url&quot;: &quot;http://a/b/c&quot;  }" mimeType="application/json" doc:name="Set JSON Payload"/>
        <json:json-to-object-transformer returnClass="java.util.Map" doc:name="JSON to Object"/>
        <dw:transform-message metadata:id="3dad12b9-422d-4c59-9ec6-dd220ebca9ef" doc:name="Transform Message">
            <dw:input-payload doc:sample="sample_data\flow16_HashMap_1.dwl" mimeType="application/java"/>
            <dw:set-payload><![CDATA[%dw 1.0
%namespace mes http://www.namespace1.com/test/message/1.0
%namespace mod htto://www.namespace2.com/test/model/1.0
%output application/xml
---
mes#getItemResponse: {
    mod#item : {
        (payload)
    }
}]]></dw:set-payload>
        </dw:transform-message>
        <byte-array-to-string-transformer doc:name="Byte Array to String"/>
        <logger message="Test1 Transformer output : #[payload]" level="INFO" doc:name="Logger"/>
    </sub-flow>

POC contain DataWeave script to transform Java Map to SOAP XML response

%dw 1.0
%namespace mes http://www.namespace1.com/test/message/1.0
%namespace mod htto://www.namespace2.com/test/model/1.0
%output application/xml
---
mes#getItemResponse: {
    mod#item : {
        (payload)
    }
}

Hardcoded Input Java Map :

{
  "item_id": "B0002345W45",
  "item_type": "Item Type 1",
  "item_type_name": "Item Type 1 Name",
  "item_name": "item 1 name",
  "item_summary": "item 1 summary",
  "item_brand": "Brand 1",
  "image_type_name": "SmallImage",
  "url": "http://a/b/c"
}

Output :

<mes:getItemResponse xmlns:mes="http://www.namespace1.com/test/message/1.0">
  <mod:item xmlns:mod="htto://www.namespace2.com/test/model/1.0">
    <item_id>B0002345W45</item_id>
    <item_type>Item Type 1</item_type>
    <item_type_name>Item Type 1 Name</item_type_name>
    <item_name>item 1 name</item_name>
    <item_summary>item 1 summary</item_summary>
    <item_brand>Brand 1</item_brand>
    <image_type_name>SmallImage</image_type_name>
    <url>http://a/b/c</url>
  </mod:item>
</mes:getItemResponse>
-1
votes

You can try using

{`%dw 1.0

%output application/xml

{ xmlRootElement : tagName1 : payload.DBEntryName1, tagName2 : payload.DBEntryName2 }

} `