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

Ste*_*veS 2 json mule mule-studio

我正在尝试使用json-to-xml-transformer将json消息转换为xml,但无法找到有关其用法的文档。我不需要任何数据转换,只需将json属性转换为xml标签即可。当我尝试使用转换器时,我得到的只是json中的第一个元素。

输入JSON:

{   
    "site":"mysite", 
    "erpCustno":"123", 
    "shipToState":"PA",
    "shipToZip":"16684",
    "lineInfo": [
        {
            "lineNumber": "10",
            "product": "MAT203"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

XML输出:

<?xml version='1.0'?><site>mysite</site>
Run Code Online (Sandbox Code Playgroud)

流:

<?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>
Run Code Online (Sandbox Code Playgroud)

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

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

Dav*_*sot 5

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

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

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

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

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