簡體   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