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.
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With