WSO2 - 如何使用脚本中介将此 XML 转换为 Json?

WSO2 - How to convert this XML to Json using script mediator?

我正在使用 WSO2 ESB 和 DDS 制作一个 API 从数据库读取数据,最后我得到了这个响应。如何使用脚本中介将此 XML 转换为 Json?请举个例子。

<?xml version='1.0' encoding='UTF-8'?> <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
    <employeeCollection xmlns="http://employee.us.wso2.com">
        <employee>
            <EmployeeID>17</EmployeeID>
            <FirstName>jak</FirstName>
            <LastName>123</LastName>
            <Team>ok</Team>
        </employee>
        <employee>
            <EmployeeID>18</EmployeeID>
            <FirstName>jak</FirstName>
            <LastName>123</LastName>
            <Team>ok</Team>
        </employee>
        <employee>
            <EmployeeID>19</EmployeeID>
            <FirstName>jak</FirstName>
            <LastName>123</LastName>
            <Team>ok</Team>
        </employee>

    </employeeCollection>
</soapenv:Body>

这是配置

<resource methods="POST" uri-template="/team">
  <inSequence>
     <sequence key="conf:/SendSelectWithTeam"/>
     <call>
        <endpoint>
           <address uri="https://192.168.2.165:9453/services/EmployeesDataService/" format="soap12"/>
        </endpoint>
     </call>

     <respond/>
  </inSequence>

附加问题:如何使用脚本中介器获取每个 "EmployeeID" 的值?

如果您只想将其直接转换为 json,则无需使用脚本中介程序手动完成。

您可以像这样更新 ESB 代理(或 API)的输出序列,它将响应 xml 转换为 json。

<outSequence>
     <property name="messageType" value="application/json" scope="axis2"/>
     <send/>
</outSequence>

有关详细信息,请参阅 WSO2 Docs

编辑:

在你的情况下,如果你使用 <respond> 调解器,它会绕过序列,我的建议是行不通的。你有 2 个选项来让它工作。

1) 使用发送调解器代替 callrespond 调解器。

2) 使用 loopback 中介而不是 respond 中介。

参考这个link

请更改配置文件中的代理输出序列设置,如下所示:

  <outSequence xmlns="http://ws.apache.org/ns/synapse">
   <property name="messageType" value="application/json/badgerfish" scope="axis2" type="STRING"></property>
   <send></send>
</outSequence>