代码之家  ›  专栏  ›  技术社区  ›  SteveS

为什么mule-json-to-xml转换器只提取第一个元素?

  •  2
  • SteveS  · 技术社区  · 11 年前

    我正在尝试使用json-to-xml转换器将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转换为JSON转换器)?

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

    2 回复  |  直到 11 年前
        1
  •  5
  •   David Dossot    11 年前

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

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

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

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

    但您可能仍然存在 lineInfo 大堆TBF我不确定Staxon会为此做什么。

        2
  •  0
  •   Naveen Rayappa    11 年前

    正如david所说,它需要一个格式正确的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/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 “>

    输入

    {
    "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>