Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mule: Is there simple way of converting XML to JSON

Tags:

json

xml

mule

Is there simple way of converting XML to JSON, without binding XML to a java object first?

Just like how this does it -- http://www.utilities-online.info/xmltojson/#.UrXnCvRDt0w

I don't want to maintain XML schema, if at all possible.

like image 551
user1493140 Avatar asked Dec 07 '22 04:12

user1493140


1 Answers

This what I ended up doing --

  <custom-transformer class="com.mycompany.transformer.XmlToJson" name="XmlToJson" doc:name="Java"/>

  <flow name="HTTP" doc:name="HTTP">
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="test" doc:name="HTTP" contentType="application/json"/>
    <http:outbound-endpoint exchange-pattern="request-response" method="GET" address="http://server-address.com" doc:name="service-call"/>
    <object-to-string-transformer doc:name="Object to String"/>
    <transformer ref="XmlToJson" doc:name="Transformer Reference"/>
</flow>

Here is XmlToJson.java --

import org.json.XML;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractTransformer;

public class XmlToJson extends AbstractTransformer{

@Override
protected Object doTransform(Object src, String enc)
        throws TransformerException {
    JSONObject jsonObj = XML.toJSONObject((String) src);
            return  jsonObj.toString();
}
}
like image 127
user1493140 Avatar answered Dec 15 '22 00:12

user1493140