Mule Datamapper JSON到XML转换

时间:2015-06-08 19:12:24

标签: mule datamapper

JSON输入有效负载信息,我遇到问题。

{
"name": "xyz",
"city": "california",
"serialNumber": [ 
   "T2323" , 
   "T2332" 
]
}

预期转换后的输出有效负载:

<COMPANY_ITEM_INFO>
     <COMPANY_NAME>xyz</COMPANY_NAME>
     <COMPANY_CITY>california</COMPANY_NAME>
     <Inv_Update SEGMENT="1"> 
         <SERIALNO>T2323</SERIALNO>  
     </Inv_Update SEGMENT="1">  
     <Inv_Update SEGMENT="1"> 
         <SERIALNO>T2332</SERIALNO>
     </Inv_Update SEGMENT="1">
</COMPANY_ITEM_INFO>

从Datamapper接收的实际输出仅1个元素作为输出。

<COMPANY_ITEM_INFO>
    <COMPANY_NAME>xyz</COMPANY_NAME>
    <COMPANY_CITY>california</COMPANY_NAME>
    <Inv_Update SEGMENT="1"> 
        <SERIALNO>T2323</SERIALNO> 
    </Inv_Update SEGMENT="1"> 
</COMPANY_ITEM_INFO>

数据映射代码:

Foreach&#39; Object&#39; - &gt; &#39; COMPANY_ITEM_INFO&#39;

//MEL
//START -> DO NOT REMOVE
output.__id = input.__id;
//END -> DO NOT REMOVE
output.COMPANY_NAME= input.name;
output.COMPANY_CITY= input.city;

ForEach&#39; serialNumber&#39; - &GT; &#39; Inv_Update&#39;

//MEL
//START -> DO NOT REMOVE
output.__id = input.__id;
output.__parent_id = input.__id;
//END -> DO NOT REMOVE
output.SERIALNO = input.array;

1 个答案:

答案 0 :(得分:1)

我知道你正在寻找一个DataMapper解决方案,但是如果你对替代方案开放,下面是一个Groovy实现(,因为DataMapper正在消失,现在是Weave is coming,你应该对选项开放吗?)。

<json:json-to-object-transformer
    returnClass="java.lang.Object" />

<scripting:transformer>
  <scripting:script engine="groovy"><![CDATA[
    def writer = new StringWriter() 
    def xml = new groovy.xml.MarkupBuilder(writer)

    xml.COMPANY_ITEM_INFO { 
      COMPANY_NAME(payload.name) 
      COMPANY_CITY(payload.city) 

      payload.serialNumber.each { sn ->
        Inv_Update(SEGMENT: '1') {
          SERIALNO(sn)
        }
      }
    }

    result = writer.toString()
  ]]></scripting:script>
</scripting:transformer>