问题
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. I don't need any transformation of the data, simply turning the json properties into xml tags. When I attempt to use the transformer, all I get out is the first element from the json.
Input JSON:
{
"site":"mysite",
"erpCustno":"123",
"shipToState":"PA",
"shipToZip":"16684",
"lineInfo": [
{
"lineNumber": "10",
"product": "MAT203"
}
]
}
XML Output:
<?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)?
I am using Anypoint Studio July 2014 and deploying to Mule EE 3.5.0.
回答1:
First, keep in mind that XML and JSON structures are not 1:1 compatible.
The json-to-xml-transformer
relies on Staxon's JSON to XML conversion. Looking at its doc and mapping conventions, it's clear that your input JSON can not be converted to XML without loss.
In your case, the first element of the JSON root object is used as the root of the XML document. You could workaround the issue by wrapping the input JSON with a fake root object:
{ "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. TBF I'm not sure what Staxon will do for it.
回答2:
As david said it requires a json in proper format and mule converts it perfectly . 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">
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>
来源:https://stackoverflow.com/questions/25083725/why-is-the-mule-json-to-xml-transformer-only-picking-up-the-first-element