为什么将ule子json转换为xml转换器仅拾取第一个元素? [英] Why is the mule json to xml transformer only picking up the first element?
问题描述
我正在尝试使用json-to-xml-transformer将json消息转换为xml,但无法找到有关其用法的文档.我不需要任何数据转换,只需将json属性转换为xml标签即可.当我尝试使用转换器时,我得到的只是json中的第一个元素.
I am attempting to use the json-to-xml-transformer to transform a json message to xml but have not been able to find documentation on its use. I don't need any transformation of the data, simply turning the json properties into xml tags. When I attempt to use the transformer, all I get out is the first element from the json.
输入JSON:
{
"site":"mysite",
"erpCustno":"123",
"shipToState":"PA",
"shipToZip":"16684",
"lineInfo": [
{
"lineNumber": "10",
"product": "MAT203"
}
]
}
XML输出:
<?xml version='1.0'?><site>mysite</site>
流量:
<?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)?
Does the transformer need a mapping class or can it just transform JSON directly to XML (and visa versa with the xml-to-json-transformer)?
我正在使用Anypoint Studio 2014年7月并部署到Mule EE 3.5.0.
I am using Anypoint Studio July 2014 and deploying to Mule EE 3.5.0.
推荐答案
首先,请记住XML和JSON结构不是1:1兼容的.
First, keep in mind that XML and JSON structures are not 1:1 compatible.
json-to-xml-transformer
依赖于Staxon的从JSON到XML的转换.查看其文档和映射约定,很明显,您输入的JSON无法转换为XML而不会丢失.
The json-to-xml-transformer
relies on Staxon's JSON to XML conversion. Looking at its doc and mapping conventions, it's clear that your input JSON can not be converted to XML without loss.
在您的情况下,JSON根对象的第一个元素用作XML文档的根.您可以通过使用伪造的根对象包装输入JSON来解决此问题:
In your case, the first element of the JSON root object is used as the root of the XML document. You could workaround the issue by wrapping the input JSON with a fake root object:
{
"root": {
"site": "mysite",
"erpCustno": "123",
"shipToState": "PA",
"shipToZip": "16684",
"lineInfo": [
{
"lineNumber": "10",
"product": "MAT203"
}
]
}
}
但是lineInfo
数组中的对象可能仍然存在问题. TBF我不确定Staxon会做什么.
But you may still have the issue for the object in the lineInfo
array. TBF I'm not sure what Staxon will do for it.
这篇关于为什么将ule子json转换为xml转换器仅拾取第一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!