首页 > 解决方案 > 在 mule 4 中将带有 CDATA 的 SOAP 响应转换为 JSON

问题描述

我有这种来自骡子 4 的肥皂反应的结果。

{ body:<web:getRoutesResponse xmlns:web="http://www.example.org/Bookings/"> <out><![CDATA[<?xml version="1.0"encoding="utf-8"?><ROUTELIST><ROUTEINFO> <ORIGIN>MY-KUL</ORIGIN><DESTINATION>SG-BEU</DESTINATION><ROUTEINFO></ROUTELIST>]]></out> </web:getRoutesResponse> , headers: [], attachments: [] }

我想在这个 json 输出中制作具有起点和终点的数组

[ { "origin": "MY-KUL", "destination": "SG-BEU" } ]

标签: muledataweave

解决方案


你可以试试这样的东西。与 Ale 的先前答案相同,但使用扫描来搜索某些文本模式之间的内容。

输入

{
    "body": "<web:getRoutesResponse xmlns:web=\"http: //www.example.org/Bookings/\"> <out><![CDATA[<?xml version=\"1.0\"encoding=\"utf-8\"?><ROUTELIST><ROUTEINFO> <ORIGIN>MY-KUL</ORIGIN><DESTINATION>SG-BEU</DESTINATION><ROUTEINFO></ROUTELIST>]]></out> </web:getRoutesResponse>",
    "headers": [],
    "attachments": []
}

脚本

%dw 2.0
output application/json
var a = read(payload.body, "application/xml").getRoutesResponse.out
---
[{
    origin: (a scan(/\<ORIGIN>(.+?)\<\/ORIGIN>/))[0][1],
    desination: (a scan(/\<DESTINATION>(.+?)\<\/DESTINATION>/))[0][1]
}]

输出

[
  {
    "origin": "MY-KUL",
    "desination": "SG-BEU"
  }
]

推荐阅读