Search code examples
mulemule-studiomule-elmule-module-jpa

Convert XML to JSON using Mule Custom Transformer


Hi I am working with Mule Studio and I want to create a Custom Transformer which will convert XML to Json using Google Gson Library. So in this case i have to add a Java Transformer component and i have to write a custom code for that. But the problem i am facing is i am overriding the method which accepts Input parameter as Object thats the only way i think or is their any other way which will accept Input Parameter as a XMl String. Please share a code to make it workable.

public class Transfomer extends AbstractTransformer { protected Object doTransform(Object src, String enc) throws TransformerException { // TODO Auto-generated method stub
return null; } }


Solution

  • Take a look at an existing transformer: https://github.com/mulesoft/mule/blob/mule-3.x/modules/xml/src/main/java/org/mule/module/xml/transformer/XmlPrettyPrinter.java

    • See how in the constructor the accepted source types are registered, like with: registerSourceType(DataTypeFactory.create(org.dom4j.Document.class));
    • See how in the doTransform method the generic Object src is converted.