简体   繁体   English

使用Mule Custom Transformer将XML转换为JSON

[英]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. 嗨,我正在与Mule Studio合作,我想创建一个自定义转换器,使用Google Gson库将XML转换为Json。 So in this case i have to add a Java Transformer component and i have to write a custom code for that. 因此,在这种情况下,我必须添加一个Java Transformer组件,并为此编写一个自定义代码。 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. 但是我面临的问题是我覆盖了接受Input参数作为Object的方法,这是我认为的唯一方法,或者是他们接受输入参数作为XMl字符串的任何其他方法。 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 看一下现有的转换器: 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)); 查看如何在构造函数中注册可接受的源类型,例如: registerSourceType(DataTypeFactory.create(org.dom4j.Document.class));
  • See how in the doTransform method the generic Object src is converted. 了解如何在doTransform方法中转换通用Object src

You can use either XML to JSON transformer from the Mule palette to the Mule flows. 您可以使用从Mule调色板到Mule流的XML到JSON转换器。

 <json:xml-to-json-transformer doc:name="XML to JSON"/>

OR 要么

Use DataWeave Transformation: 使用DataWeave转换:

 <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. 将Dataweave输出设置为application / json会自动执行。 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. 但是,当您将JSON转换为XML时,可能必须确保可以将JSON转换为XML格式,其中包含根元素和成员元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM