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/
You can build a custom transformer[1] based on json-lib[2].
[1] http://www.mulesoft.org/documentation/display/current/Creating+Custom+Transformers
[2] http://json-lib.sourceforge.net/
Here's the implementation using link 1 from Pontus and FasterXML/Jackson
import java.util.List;
import org.codehaus.jackson.map.ObjectMapper;
import org.mule.api.MuleMessage;
import org.mule.api.transformer.DiscoverableTransformer;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractMessageTransformer;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class XmlToJsonTransformer extends AbstractMessageTransformer implements
DiscoverableTransformer {
public Object transformMessage(MuleMessage message, String outputEncoding)
throws TransformerException {
try {
String xml = (String) message.getPayload();
XmlMapper xmlMapper = new XmlMapper();
List entries = xmlMapper.readValue(xml, List.class);
ObjectMapper jsonMapper = new ObjectMapper();
String json = jsonMapper.writeValueAsString(entries);
return json;
} catch (Exception e) {
System.out.println("Error: " + e);
e.printStackTrace();
}
return null;
}
@Override
public int getPriorityWeighting() {
return 0;
}
@Override
public void setPriorityWeighting(int weighting) {
}
}
The flow could look something like this:
<flow name="xmltojsontransformerFlow1" doc:name="xmltojsontransformerFlow1">
<quartz:inbound-endpoint jobName="job"
repeatInterval="0" repeatCount="0" startDelay="300" responseTimeout="10000"
doc:name="Quartz">
<quartz:event-generator-job />
</quartz:inbound-endpoint>
<parse-template
location="/Users/tjs/MuleStudio/workspace3/xmltojsontransformer/src/main/resources/cd_catalog.xml"
doc:name="Parse Template" />
<custom-transformer class="cc.notsoclever.mule.XmlToJsonTransformer" doc:name="XmlToJson"/>
<logger message="#[payload]" level="INFO" doc:name="Logger" />
</flow>
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();
}
}
Use the XML to Json transformer in Mule ESB or use the Dataweave transformer.