Question

I am trying to convert xml response to JSON as per the below flow

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" xmlns:https="http://www.mulesoft.org/schema/mule/https" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd
http://www.mulesoft.org/schema/mule/https http://www.mulesoft.org/schema/mule/https/current/mule-https.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
    <data-mapper:config name="json_to_xml" transformationGraphPath="json_to_xml.grf" doc:name="json_to_xml"/>
    <custom-transformer class="org.hhmi.transformer.XmlToJson" name="XmlToJson" doc:name="Java" mimeType="application/json"/>
    <flow name="sumtotalFlow1" doc:name="sumtotalFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="9119" doc:name="HTTP"/>
        <logger level="INFO" doc:name="Logger"/>
        <data-mapper:transform config-ref="json_to_xml" doc:name="JSON To XML"/>
        <logger level="DEBUG" doc:name="Logger" message="#[message.payload]"/>
        <https:outbound-endpoint exchange-pattern="request-response" host="stage.sumtotalsystems.com" port="443" path="webservice/services/WFM3Service" method="POST" doc:name="HTTP" contentType="text/xml"/>
        <logger message="SOAP Resonse from SumTotal #[message.payload]" level="DEBUG" doc:name="Logger"/>
        <mulexml:xslt-transformer mimeType="text/xml" maxIdleTransformers="2" maxActiveTransformers="5" xsl-file="XmlResponse.xsl" doc:name="XSLT"/>
        <object-to-string-transformer mimeType="text/xml" doc:name="Object to String"/>
     <transformer ref="XmlToJson" doc:name="XmlToJson"/>
    </flow>
</mule>

The flow is working till the Object to String Transformer. Below is Custom Transformer for XmlToJson conversion. It seems to hang at the xmlSearlizer.read(xml) line .

import net.sf.json.JSON;
import net.sf.json.xml.XMLSerializer;

import org.apache.log4j.Logger;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractTransformer;

public class XmlToJson extends AbstractTransformer {
    public static Logger logger = Logger.getLogger(XmlToJson.class);

    @Override
    protected Object doTransform(Object src, String enc)
            throws TransformerException {

        // String xml = (String) src;
        logger.debug("XmlToJson Payload " + src.toString());
        System.out.println("XmlToJson Payload " + src.toString());
        XMLSerializer xmlSerializer = new XMLSerializer();
        xmlSerializer.setSkipNamespaces(true);
        xmlSerializer.setTrimSpaces(true);
        xmlSerializer.setRemoveNamespacePrefixFromElements(true);
        JSON json = xmlSerializer.read(src.toString());
        System.out.println("XmlToJson json.toString() " + json.toString());
        return json.toString();
    }
}

Any help will be greatly appreciated. Thanks.

Was it helpful?

Solution

If you use the XMLSerializer from net.sf.json-lib you have to bring the optional XOM dependency as well otherwise execution will fail.

Use:

    <dependency>
        <groupId>net.sf.json-lib</groupId>
        <artifactId>json-lib</artifactId>
        <version>2.4</version>
        <classifier>jdk15</classifier>
    </dependency>
    <dependency>
        <groupId>xom</groupId>
        <artifactId>xom</artifactId>
        <version>1.1</version>
    </dependency>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top