我有XML输入,需要转换为java映射,用作CRM在线连接器(创建帐户)的输入。
示例输入:我只给出了我面临问题的xml的一部分
<account>
<identification>
<accountId>String</accountId>
</identification>
<addresses>
<address>
<type>type1</type>
<name>name1</name>
<street>street1</street>
<communication>
<phone>phone1</phone>
<phoneExtension>phoneExtension1</phoneExtension>
<phoneCountry>phoneCountry1</phoneCountry>
<fax>fax1</fax>
<faxExtension>faxExtension1</faxExtension>
<faxCountry>faxCountry1</faxCountry>
<email>email1</email>
<website>website1</website>
</communication>
</address>
<address>
<type>type2</type>
<name>name2</name>
<street>street2</street>
<communication>
<phone>phone2</phone>
<phoneExtension>phoneExtension2</phoneExtension>
<phoneCountry>phoneCountry2</phoneCountry>
<fax>fax2</fax>
<faxExtension>faxExtension2</faxExtension>
<faxCountry>faxCountry2</faxCountry>
<email>email1</email>
<website>website2</website>
</communication>
</address>
<address>
<type>type3</type>
<name>name3</name>
<street>street3</street>
<communication>
<phone>phone3</phone>
<phoneExtension>phoneExtension3</phoneExtension>
<phoneCountry>phoneCountry3</phoneCountry>
<fax>fax3</fax>
<faxExtension>faxExtension3</faxExtension>
<faxCountry>faxCountry3</faxCountry>
<email>email3</email>
<website>website3</website>
</communication>
</address>
</addresses>
正如你所看到的,我有三个地址,这是一个地址数组,所有都有相同的标签。
我想把这个xml转换成java地图,在CRM Dynamics上创建一个在线帐户。但问题是,在客户关系管理中,我们有完全不同的结构。我们有键值对形式的地址。所以从上面的地址输入我们得到下面的映射
address1_type : type1
address1_name : name1
address1_street : street1
address1_phone : phone1
address1_phoneExtension : phoneExtension1
address1_phoneCountry : phoneCountry1
address1_fax : fax1
address1_faxExtension : faxExtension1
address1_faxCountry : faxCountry1
address1_email : email1
address1_website : website1
address2_type : type2
address2_name : name2
address2_street : street2
address2_phone : phone2
address2_phoneExtension : phoneExtension2
address2_phoneCountry : phoneCountry2
address2_fax : fax2
address2_faxExtension : faxExtension2
address2_faxCountry : faxCountry2
address2_email : email2
address2_website : website2
address3_type : type3
address3_name : name3
address3_street : street3
address3_phone : phone3
address3_phoneExtension : phoneExtension3
address3_phoneCountry : phoneCountry3
address3_fax : fax3
address3_faxExtension : faxExtension3
address3_faxCountry : faxCountry3
address3_email : email3
address3_website : website3
是否有一种方法将输入地址数组转换为所需的映射键值?如果可能的话,我正在寻找一种方法来使用dataweave。
我已经找到解决办法了。这可能对某人有帮助
转换可以在循环遍历列表并在dataweave中创建具有特定索引的动态映射键。
%dw 1.0
%output application/java
---
{ //tells DW that output should be object i.e. HashMap
( //tells DW to use key-value from below mapping
payload.account.addresses map ((value,index) -> {
("address" ++ (index+1) ++ "_type"): value.type, // Parenthesis around key tells DW to evaluate contents and use the result as key
("address" ++ (index+1) ++ "_name"): value.name,
("address" ++ (index+1) ++ "_street"): value.street
})
)
}