Mule:有简单的方法将XML转换为JSON

时间:2013-12-21 19:09:06

标签: xml json mule

是否有简单的方法将XML转换为JSON,而不首先将XML绑定到java对象?

就像这样做 - http://www.utilities-online.info/xmltojson/#.UrXnCvRDt0w

如果可能的话,我不想维护XML架构。

4 个答案:

答案 0 :(得分:7)

这就是我最终做的事情 -

  <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>

这是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();
}
}

答案 1 :(得分:5)

答案 2 :(得分:3)

以下是使用Pontus和FasterXML / Jackson

的链接1的实现
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) {
}
}

流程看起来像这样:

<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>

答案 3 :(得分:0)

在Mule ESB中使用XML to Json转换器或使用Dataweave转换器。