Mule 数据文件夹 JSON 到 XML 转换

Mule Datamapper JSON to XML Transformation

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>

Datamapper 代码:

Foreach 'Object' -> 'COMPANY_ITEM_INFO'

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

ForEach 'serialNumber' -> 'Inv_Update'

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

我知道您正在寻找 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>