我正在尝试使用 json-to-xml-transformer 将 json 消息转换为 xml,但无法找到有关其使用的文档。我不需要对数据进行任何转换,只需将 json 属性转换为 xml 标签即可。当我尝试使用转换器时,我得到的只是 json 中的第一个元素。
输入 JSON:
{
"site":"mysite",
"erpCustno":"123",
"shipToState":"PA",
"shipToZip":"16684",
"lineInfo": [
{
"lineNumber": "10",
"product": "MAT203"
}
]
}
XML 输出:
<?xml version='1.0'?><site>mysite</site>
Flow:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.5.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
<flow name="newpigrestFlow1" doc:name="newpigrestFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8087" doc:name="HTTP"/>
<json:json-to-xml-transformer mimeType="text/xml" doc:name="JSON to XML" ignoreBadInput="true"/>
</flow>
</mule>
转换器是否需要映射类,或者可以直接将 JSON 转换为 XML(反之亦然,使用 xml-to-json-transformer)?
我正在使用 Anypoint Studio 2014 年 7 月并部署到 Mule EE 3.5.0。
首先,请记住 XML 和 JSON 结构不是 1:1 兼容的。
The json-to-xml-transformer
依赖 Staxon 的JSON 到 XML 的转换 https://github.com/beckchr/staxon/wiki/Converting-JSON-to-XML。查看它的文档和映射约定 https://github.com/beckchr/staxon/wiki/Mapping-Convention,很明显,您的输入 JSON 无法无损地转换为 XML。
在您的例子中,JSON 根对象的第一个元素用作 XML 文档的根。您可以通过使用假根对象包装输入 JSON 来解决此问题:
{
"root": {
"site": "mysite",
"erpCustno": "123",
"shipToState": "PA",
"shipToZip": "16684",
"lineInfo": [
{
"lineNumber": "10",
"product": "MAT203"
}
]
}
}
但您可能仍然遇到对象中的问题lineInfo
大批。 TBF 我不确定 Staxon 会为此做什么。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)