1
votes

I tried to convert my json data into xml format. But Only half of data is convert into xml.

My payload is

{"orders":[{"orderName":"Laptop","price":34000,"Date":"2014/01/12","Type":"DELL","stock":52,"code":"152666AS"},

    {"orderName":"Chip","price":345,"Date":"2014/02/20","Type":"DELL","stock":50,"code":"152666AW"},

    {"orderName":"Laptop1","price":35000,"Date":"2015/02/13","Type":"DELL1","stock":51,"code":"152666AX"}]}

But in output I got only one json item

    <?xml version='1.0'?>
<orders>
<orderName>Laptop</orderName>
<price>34000</price>
<Date>2014/01/12</Date>
<Type>DELL</Type>
<stock>52</stock>
<code>152666AW</code>
</orders>

My flow is as follow

<flow name="testFlow">
        <http:listener config-ref="HTTP_Quickbook" path="/" doc:name="HTTP"/>
        <connector-test:my-processor config-ref="ConnectorTest__Configuration_type_strategy" content="APP" doc:name="ConnectorTest"/>
        <json:json-to-xml-transformer  mimeType="application/json" doc:name="JSON to XML"/>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>

</flow>

I need whole json string in xml format . What I have to change?

I tested with creating custom transformer.. My custom transformer is as follow

InputStream input = new FileInputStream(new File("test.json"));
            OutputStream output = System.out;
            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
            inputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);
            XMLEventReader reader = inputFactory.createXMLEventReader(input);

            XMLOutputFactory outputFactory = new JsonXMLOutputFactory();
            outputFactory.setProperty(JsonXMLOutputFactory.PROP_PRETTY_PRINT, true);
            XMLEventWriter writer = outputFactory.createXMLEventWriter(output);

            writer = new XMLMultipleEventWriter(writer, false,"/orders");

            writer.add(reader);

            reader.close();
            writer.close();

Now I got following error

com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '{' (code 123) in prolog; expected '<'
 at [row,col {unknown-source}]: [1,1]

How to solve this?

3

3 Answers

1
votes

i can suggest you one thing to make your requirement to work.

if you are want to generate the xml from json.

do the below

add set-payload component to you flow explicitly specify the value as

{"root":#[payload]}

it will work and i could able to generate the xml data out of the sample data you pasted.

please do try and post your status

1
votes

Your issue comes from the transformation of the array of orders that fails and returns only one entry.

Behind the scene, the json:json-to-xml-transformer uses Staxon for XML to JSON transformation. Here is the doc on array transformation in Staxon: https://github.com/beckchr/staxon/wiki/Mastering-Arrays

We can see in the Mule source that PROP_MULTIPLE_PI is false and PROP_AUTO_ARRAY is not set to true, therefore only one item of the array is considered, the others are dropped.

So the best is for you to write your own transformer either using Staxon too, configured with the array handling settings you want, or using Groovy XML Markup builder to generate the XML in a nice and easy way.

0
votes

For what you're looking for json

{
      "ocs:price": {
        "@exponent": "-1",
        "#text": "2"
      }
    }

That's the format to convert for example that from JSON to XML like this :

<ocs:price exponent="-1">2</ocs:price> 

In java/groovy script I used something like this to convert it

import net.sf.json.JSON
import net.sf.json.JSONSerializer
import net.sf.json.xml.XMLSerializer

String str = '''{
  "ocs:price": {
    "@exponent": "-1",
    "#text": "2"
  }
}'''
JSON json = JSONSerializer.toJSON( str )
XMLSerializer xmlSerializer = new XMLSerializer()
xmlSerializer.setTypeHintsCompatibility( false )
String xml = xmlSerializer.write( json )
System.out.println(xml)