繁体   English   中英

为什么将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中的第一个元素。

输入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)?

我正在使用Anypoint Studio 2014年7月并部署到Mule EE 3.5.0。

首先,请记住XML和JSON结构不是1:1兼容的。

json-to-xml-transformer依赖于Staxon的JSON到XML的转换 查看它的doc和映射约定 ,很显然,输入JSON不能无损地转换为XML。

在您的情况下,JSON根对象的第一个元素用作XML文档的根。 您可以通过使用伪造的根对象包装输入JSON来解决此问题:

{
    "root": {
        "site": "mysite",
        "erpCustno": "123",
        "shipToState": "PA",
        "shipToZip": "16684",
        "lineInfo": [
            {
                "lineNumber": "10",
                "product": "MAT203"
            }
        ]
    }
}

但是lineInfo数组中的对象可能仍然存在问题。 TBF我不确定Staxon会做什么。

正如大卫所说,它需要一个格式正确的json,并且ule子可以将其完美转换。 我已经分享了流程细节。

<?xml version="1.0" encoding="UTF-8"?>

http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/核心/当前/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 http://www.mulesoft.org/schema/mule/jms http: //www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/ file / current / mule-file.xsd “>

输入

{
"root": {
    "site": "mysite",
    "erpCustno": "123",
    "shipToState": "PA",
    "shipToZip": "16684",
    "lineInfo": [
        {
            "lineNumber": "10",
            "product": "MAT203"
        }
    ]
}

}

产量

<?xml version='1.0'?><root><site>mysite</site><erpCustno>123</erpCustno><shipToState>PA</shipToState><shipToZip>16684</shipToZip><lineInfo><lineNumber>10</lineNumber><product>MAT203</product></lineInfo></root>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM