简体   繁体   English

为什么将ule子json转换为xml转换器仅拾取第一个元素?

[英]Why is the mule json to xml transformer only picking up the first element?

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. 我正在尝试使用json-to-xml-transformer将json消息转换为xml,但无法找到有关其用法的文档。 I don't need any transformation of the data, simply turning the json properties into xml tags. 我不需要任何数据转换,只需将json属性转换为xml标签即可。 When I attempt to use the transformer, all I get out is the first element from the json. 当我尝试使用转换器时,我得到的只是json中的第一个元素。

Input JSON: 输入JSON:

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

XML Output: 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>

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)? 转换器是否需要映射类,还是可以将JSON直接转换为XML(反之亦然,使用xml-to-json-transformer)?

I am using Anypoint Studio July 2014 and deploying to Mule EE 3.5.0. 我正在使用Anypoint Studio 2014年7月并部署到Mule EE 3.5.0。

First, keep in mind that XML and JSON structures are not 1:1 compatible. 首先,请记住XML和JSON结构不是1:1兼容的。

The json-to-xml-transformer relies on Staxon's JSON to XML conversion . json-to-xml-transformer依赖于Staxon的JSON到XML的转换 Looking at its doc and mapping conventions , it's clear that your input JSON can not be converted to XML without loss. 查看它的doc和映射约定 ,很显然,输入JSON不能无损地转换为XML。

In your case, the first element of the JSON root object is used as the root of the XML document. 在您的情况下,JSON根对象的第一个元素用作XML文档的根。 You could workaround the issue by wrapping the input JSON with a fake root object: 您可以通过使用伪造的根对象包装输入JSON来解决此问题:

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

But you may still have the issue for the object in the lineInfo array. 但是lineInfo数组中的对象可能仍然存在问题。 TBF I'm not sure what Staxon will do for it. TBF我不确定Staxon会做什么。

As david said it requires a json in proper format and mule converts it perfectly . 正如大卫所说,它需要一个格式正确的json,并且ule子可以将其完美转换。 i have shared the flow details. 我已经分享了流程细节。

Flow

<?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/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 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 "> 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 “>

Input 输入

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

} }

Output 产量

<?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