【问题标题】:Why is the mule json to xml transformer only picking up the first element?为什么 mule json to xml 转换器只拾取第一个元素?
【发布时间】:2023-03-30 21:04:01
【问题描述】:

我正在尝试使用 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。

【问题讨论】:

    标签: json mule mule-studio


    【解决方案1】:

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

    json-to-xml-transformer 依赖于 Staxon 的 JSON to XML conversion。查看它的文档和mapping conventions,很明显您的输入 JSON 无法毫无损失地转换为 XML。

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

    { “根”: { “网站”:“我的网站”, “erpCustno”:“123”, "shipToState": "PA", "shipToZip": "16684", “线路信息”:[ { "lineNumber": "10", “产品”:“MAT203” } ] } }

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

    【讨论】:

    • +1 - 如果您自己实现 Staxon,您可以将 virtualRoot 添加到您的 JsonXMLConfigBuilder
    • 谢谢。添加根元素有效。此外,我的 lineInfo 对象也能正常工作并且转换正确。
    【解决方案2】:

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

    流程

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

    http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/corehttp://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/httphttp://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/jsonhttp://www.mulesoft.org/schema/mule/json/current/mule-json.xsd http://www.mulesoft.org/schema/mule/jmshttp://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd http://www.mulesoft.org/schema/mule/filehttp://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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-14
      • 1970-01-01
      • 1970-01-01
      • 2022-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多