Search code examples
wso2cdatawso2-enterprise-integratorwso2-esb

WSO2 ESB/EI : How to get Original JSON body into XML Payload


I'm implementing WSO2 EI-6.4.0 for the project and I want to convert JSON to XML. Below issue came when HTML tags contain in the JSON response.

Example:

Input:

{
  "departurePoint": "1: Galle, Sri Lanka<br />2: Hikkaduwa, Sri Lanka<br />3: Unawatuna, Sri Lanka<br />4: Ahangama, Sri Lanka<br />5: Midigama Beach, Sri Lanka<br />6: Weligama, Sri Lanka<br />7: Mirissa, Sri Lanka<br /><br>Traveler pickup is offered<br/>Free Pick up locations : Hikkaduwa , Galle ,Unawatuna , Ahangama , Midigama , Weligama , Mirissa<br><br>"
}

Original Output:

<departurePoint>
    1: Galle, Sri Lanka
    <br />2: Hikkaduwa, Sri Lanka
    <br />3: Unawatuna, Sri Lanka
    <br />4: Ahangama, Sri Lanka
    <br />5: Midigama Beach, Sri Lanka
    <br />6: Weligama, Sri Lanka
    <br />7: Mirissa, Sri Lanka
    <br />
    <br>Traveler pickup is offered
        <br/>Free Pick up locations : Hikkaduwa , Galle ,Unawatuna , Ahangama , Midigama , Weligama , Mirissa
        <br>
    <br>
</departurePoint>

Expected Output:

<departurePoint>
    <![CDATA[1: Galle, Sri Lanka<br />2: Hikkaduwa, Sri Lanka<br />3: Unawatuna, Sri Lanka<br />4: Ahangama, Sri Lanka<br />5: Midigama Beach, Sri Lanka<br />6: Weligama, Sri Lanka<br />7: Mirissa, Sri Lanka<br /><br>Traveler pickup is offered<br/>Free Pick up locations : Hikkaduwa , Galle ,Unawatuna , Ahangama , Midigama , Weligama , Mirissa<br><br>]]>
</departurePoint>

I couldn't find the way to insert CDATA for the sequence.


Solution

  • In order to achieve your requirement, you can use the Payload factory mediator as well. Following is a sample mediation to achieve your requirements. Here a registry resource is used with the CDATA element to avoid a limitation in the payload factory mediator

    Proxy configuration

    <?xml version="1.0" encoding="UTF-8"?>
    <proxy xmlns="http://ws.apache.org/ns/synapse"
           name="sampleProxy"
           startOnLoad="true"
           statistics="disable"
           trace="disable"
           transports="http,https">
       <target>
          <inSequence>
             <call>
                <endpoint>
                   <http uri-template="http://www.mocky.io/v2/5dbdc767330000678f16a289"/>
                </endpoint>
             </call>
             <log level="custom">
                <property expression="$body/jsonObject/departurePoint" name="test"/>
             </log>
             <payloadFactory media-type="xml">
                <format key="conf:/test/format.xml"/>
                <args>
                   <arg evaluator="xml" expression="$body/jsonObject/departurePoint"/>
                </args>
             </payloadFactory>
             <respond/>
          </inSequence>
       </target>
       <description/>
    </proxy>
    

    Registry resource (format.xml)

    <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
    <soap:Body>
         <departurePoint ><![CDATA[$1]]></departurePoint>
    </soap:Body>
    

    Even with this configuration, you will not get the expected output. The reason for this is WSO2 EI uses STAX parser and by default, the STAX parser is in non-coalescing mode. To correct that, you need to do the following

    1. create a file called XMLInputFactory.properties inside <EI_HOME>

    2. Add the following content into the file

    javax.xml.stream.isCoalescing=false