简体   繁体   English

如何在m子中使用dataweave将HashMap转换为XML

[英]How to convert HashMap to XML using dataweave in mule

I want to convert DB records into XML. 我想将数据库记录转换为XML。 Select query from DB records is giving LinkedHashMap as payload. 从数据库记录中选择查询将LinkedHashMap用作有效负载。 Now I want to use dataweave to transform this into XML. 现在,我想使用dataweave将其转换为XML。 I am pretty new to mule so a bit lost. 我对m子很陌生,所以有点迷路。 What all steps do I need to follow to do this? 为此,我需要遵循什么所有步骤?

Thank you. 谢谢。

As informed by Victor please go through Dataweave Documentation which is the Data transformation component in Mule. 根据Victor的通知,请仔细阅读Dataweave文档,它是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. 以下脚本是一个示例,您可以参考该示例将其添加到m子代码中的数据编织组件中。

%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. Map函数将遍历数组或JSON中的复杂xml或数组。 Here for your DB response you can consider as every row in your DB resulset will be iterated 在这里,您可以考虑数据库响应,因为数据库resulset中的每一行都会被迭代

Transform Java Map to XML using DataWeave : 使用DataWeave将Java Map转换为XML:

You can try below POC : 您可以在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 POC包含DataWeave脚本以将Java Map转换为SOAP XML响应

%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 : 硬编码输入Java映射:

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

You can try using 您可以尝试使用

{`%dw 1.0 {`%dw 1.0

%output application/xml %输出应用程序/ XML

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

} ` }`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM