WSO2 ESB从XML(多行)到JSON(没有包装)

时间:2018-05-14 21:05:04

标签: wso2 wso2esb wso2ei

我希望在这里找到帮助......以下是具体案例: 我从我的端点获取此XML:

<Entries>
    <Entry>
        <Customer>1</Customer>
    </Entry>
    <Entry>
        <Customer>2</Customer>
    </Entry>
<Entries>

我可以通过更改Property messageType轻松地将此XML转换为JSON,这将导致:

{"Entries":{"Entry":[{"Customer": 1}, {"Customer": 2}]}}

这是我想要的,作为JSON结果(没有包装器):

[{"Customer": 1}, {"Customer": 2}]

有人知道怎么做?
非常感谢提前!

2 个答案:

答案 0 :(得分:1)

我认为你首先要操纵你的xml(可能是用xslt介体)来这样格式化

<jsonArray>
    <Customer>1</Customer>
    <Customer>2</Customer>
</jsonArray>

然后我想你会得到你期望的输出。

例如,以下xslt可以完成这项工作

<xsl:stylesheet exclude-result-prefixes="xsl" version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output encoding="utf-8" indent="yes" method="xml" omit-xml-declaration="yes"/>     
    <xsl:template match="/Entries">
        <xsl:element name="jsonArray">
            <xsl:copy-of select="./Entry/Customer" />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

答案 1 :(得分:0)

谢谢Anuruddha和Nicolas! 你的答案激励着我,我想分享我的所作所为。

我创建了这个序列,我重复使用了序列介体:

<sequence name="toJSON" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
    <property name="messageType" scope="axis2" type="STRING" value="application/json"/>
    <property name="result" scope="default" type="STRING" expression="json-eval($.Entries.Entry)"/>
    <payloadFactory media-type="json">
        <format>$1</format>
        <args>
            <arg evaluator="xml" expression="$ctx:result"/>
        </args>
    </payloadFactory>
</sequence>

它工作得很好我甚至改变了所有代理的顺序,甚至那些只返回1个结果的代码。

非常感谢你的灯!!