在GateWayScript中解析XML响应 [英] Parsing XML Response In GateWayScript

查看:96
本文介绍了在GateWayScript中解析XML响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是API连接的新手...我有一个用例,其中我必须根据特定条件以XML格式合并来自两个端点的响应.

Hi i am new to API connect ... i have a use case where i have to merge responses coming from two endpoints in XML format based on certain conditions.

我在组装部分的流程是这样

My flow in the assemble section is like this

1) INVOKE

(我做我的 首次服务电话 并捕获响应 在自定义' Response 对象变量'-XMLResponse1

(i make my first service call and capture the response in a custom 'Response object varibale' -XMLResponse1

2) INVOKE

(我做我的 第二次服务电话 在这里我不使用任何 自定义'响应对象变量' 相反,我正在使用apim.getvaribale('message.body')来获取响应

(i make my second service call and here i am not using any custom 'Response object varibale' Instead i am using apim.getvaribale('message.body') to get the response

3)网关脚本

在这里,我想编写我的脚本来解析xml并合并两个响应 并将合并后的响应发送回消费者

Here i want to write my script for parsing the xml and merging the two responses and send back the merged response to the consumer

我观察到当我尝试如下所示捕获XML响应时,该响应未在自定义Response对象变量中捕获

I observed that the xml response is not getting captured in a custom Response object variable when i try to capture it like below

var test1 = apim.getvariable('XMLResponse1'); test1.item(0).childNodes

var test1= apim.getvariable('XMLResponse1'); test1.item(0).childNodes

它引发了这样的异常

test1.item不是函数

现在针对第二个响应,如下所示,其中我没有在自定义Response对象变量中捕获响应,因此效果很好

now for the second response like below where i am not capturing the response in a custom Response object variable it works well

var test2 = apim.getvariable('message.body');

var test2= apim.getvariable('message.body');

我的问题:

1)如何在自定义Response对象变量中捕获xml响应?

1)How do i capture the xml responses in a custom Response object variable?

2)如何将响应解析为javascript对象? api connect是否支持任何库?

2)How can i parse the response into a javascript object? are there any libraries supported in api connect?

推荐答案

以下是从IBM社区中找到的样本.希望对您有帮助.

Below is the samples found from IBM community. Hope this may help you.

**** Sample XML ****

 <Routing>        
    <partner name="Partner A" key="1">
        <from_ID>PartnerA-KEY1-INT</from_ID>
        <to_ID>PartnerA-KEY1-EXT</to_ID>
        <destination>PartnerA-KEY1-DESTINATION</destination>
    </partner>  
    <partner name="Partner B" key="2">
        <from_ID>PartnerB-KEY2-INT</from_ID>
        <to_ID>PartnerB-KEY2-EXT</to_ID>
        <destination>PartnerB-KEY2-DESTINATION</destination>
    </partner>
    <partner name="Partner C" key="3">
        <from_ID>PartnerC-KEY3-INT</from_ID>
        <to_ID>PartnerC-KEY3-EXT</to_ID>
        <destination>PartnerC-KEY3-DESTINATION</destination>
    </partner>  
</Routing>

 **** Corresponing Gateway Script *****

var response = apim.getvariable('XMLResponse1.body');
var objType = response.item(0);
var string  = objType.getElementsByTagName("partner").item(0).getElementsByTagName("from_ID").item(0).textContent;

输出--->字符串= PartnerA-KEY1-INT

output ---> string = PartnerA-KEY1-INT

这篇关于在GateWayScript中解析XML响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆