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; } }
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
registerSourceType(DataTypeFactory.create(org.dom4j.Document.class));
doTransform
method the generic Object src
is converted. You can use either XML to JSON transformer from the Mule palette to the Mule flows.
<json:xml-to-json-transformer doc:name="XML to JSON"/>
OR
Use DataWeave Transformation:
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
payload]]></dw:set-payload>
</dw:transform-message>
Setting Dataweave output as application/json does the automatically. However when you do JSON to XML you might have to make sure the JSON can be converted to XML form with root element and member elements within.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.