WSO2 ESB / EI:如何将原始JSON正文转换为XML有效负载

时间:2019-10-29 07:22:49

标签: wso2 wso2esb cdata wso2ei

我正在为该项目实施WSO2 EI-6.4.0,我想将JSON转换为XML。 当HTML标记包含在JSON响应中时,出现以下问题。

示例:

输入:

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

预期输出:

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

我找不到为序列插入CDATA的方法。

1 个答案:

答案 0 :(得分:2)

为了满足您的要求,您也可以使用Payload工厂调解器。以下是实现您的要求的示例中介。这里,注册表资源与CDATA元素一起使用,以避免有效负载工厂介体受到限制

代理配置

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

注册资源(format.xml)

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

即使使用此配置,也不会获得预期的输出。原因是WSO2 EI使用STAX解析器,并且默认情况下,STAX解析器处于非协作模式。要纠正这一点,您需要执行以下操作

  1. <EI_HOME>

  2. 中创建一个名为XMLInputFactory.properties的文件
  3. 将以下内容添加到文件中

javax.xml.stream.isCoalescing = false