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

和自甴很熟 提交于 2020-01-06 15:51:12

问题


Im using WSO2 ESB and DDS to make an API to read data from database and finally I got this respone. How to convert this XML to Json using script mediator? Give me an example please.

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

And this is the configuration

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

Additional question: How to get the value of the each of "EmployeeID" using script mediator?


回答1:


If you just want to convert this directly to json, you don't need to do it manually using script mediator.

You can update the out sequence of your ESB proxy (or API) like this and it will convert the response xml to json.

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

See WSO2 Docs for more details.

Edit:

In your case, if you use <respond> mediator, it will bypass out sequence, and my suggestion won't work. You have 2 options to get it working.

1) Use Send mediator instead of call and respond mediators.

or

2) Use loopback mediator instead of respond mediator.




回答2:


Refer this link

How to convert SOAP response with xsi values to json in WSO2esb

Please change the proxy outSequence settings in config file like below:

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


来源:https://stackoverflow.com/questions/39034781/wso2-how-to-convert-this-xml-to-json-using-script-mediator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!