如何在SAP Netweaver网关HTTP适配器中使用响应?

时间:2015-05-22 11:25:12

标签: javascript json xml ibm-mobilefirst mobilefirst-adapters

我正在使用SAP Netweaver Gateway HTTP Adapter集成SAP和IBM MobileFirst。 我能够成功连接到SAP后端系统,并且我以xml格式获取数据。这是我得到的回应:

{
"RETURN": "<?xml version=\"1.0\" encoding=\"utf-8\"?><app:service xml:base=\"http:\/\/sapecc.mss.com:8000\/sap\/opu\/sdata\/iwfnd\/SampleUserProcessing\/\" xmlns:app=\"http:\/\/www.w3.org\/2007\/app\" xmlns:atom=\"http:\/\/www.w3.org\/2005\/Atom\" xmlns:sap=\"http:\/\/www.sap.com\/Protocols\/SAPData\" xmlns:gp=\"http:\/\/www.sap.com\/Protocols\/SAPData\/GenericPlayer\" xmlns:m=\"http:\/\/schemas.microsoft.com\/ado\/2007\/08\/dataservices\/metadata\"><app:workspace sap:semantics=\"things\"><atom:title>Things<\/atom:title><\/app:workspace><app:workspace sap:semantics=\"data\"><atom:title>Data<\/atom:title><app:collection href=\"RoleCollection\" sap:creatable=\"true\" sap:updatable=\"true\" sap:deletable=\"true\"><atom:title>RoleCollection<\/atom:title><sap:member-title>Role<\/sap:member-title><\/app:collection><app:collection href=\"ImageCollection\" sap:creatable=\"true\" sap:updatable=\"true\" sap:deletable=\"true\"><atom:title>ImageCollection<\/atom:title><sap:member-title>Image<\/sap:member-title><\/app:collection><app:collection href=\"EmailCollection\" sap:creatable=\"true\" sap:updatable=\"true\" sap:deletable=\"true\"><atom:title>EmailCollection<\/atom:title><sap:member-title>Email<\/sap:member-title><\/app:collection><app:collection href=\"ActivitygroupCollection\" sap:creatable=\"true\" sap:updatable=\"true\" sap:deletable=\"true\"><atom:title>ActivitygroupCollection<\/atom:title><sap:member-title>Activitygroup<\/sap:member-title><\/app:collection><app:collection href=\"UserCollection\" sap:creatable=\"true\" sap:updatable=\"true\" sap:deletable=\"true\"><atom:title>UserCollection<\/atom:title><sap:icon>icon<\/sap:icon><sap:member-title>User<\/sap:member-title><atom:link href=\"SubscriptionCollection\" rel=\"http:\/\/www.sap.com\/Protocols\/SAPData\/rel#subscribe\"\/><\/app:collection><app:collection href=\"UserXnotificationCollection\" sap:creatable=\"false\" sap:updatable=\"false\" sap:deletable=\"false\"><atom:title>UserxnotificationCollection<\/atom:title><sap:member-title>UserXnotification<\/sap:member-title><\/app:collection><app:collection href=\"SubscriptionCollection\" sap:semantics=\"subscriptions\" sap:creatable=\"true\" sap:updatable=\"false\" sap:deletable=\"true\"><atom:title>Subscriptions<\/atom:title><sap:icon>\/sap\/public\/opu\/resources\/sap\/iwfnd\/subscription\/icon_subscription.png<\/sap:icon><sap:member-title>Subscription<\/sap:member-title><gp:collectionLayout display-order=\"1000\" top-level=\"true\"\/><\/app:collection><\/app:workspace><app:workspace sap:semantics=\"subscriptions&amp;notifications\"><atom:title>Subscriptions &amp; Notifications<\/atom:title><\/app:workspace><\/app:service>",
    "isSuccessful": true,
    "responseHeaders": {
      "content-type": "application\/xml",
      "dataserviceversion": "2.0",
      "server": "SAP NetWeaver Application Server \/ ABAP 731",
      "x-sap-odata-extension-version": "0.9;gp=0.8"
    },
    "statusCode": 200,
    "statusReason": "OK"
    }

现在我想从xml中选择一个特定的值。任何人都可以帮助我解决这个问题吗?

function retrieveTravelAgencyProperty() {
var request = {
    CollectionName: "SampleUserProcessing",

};
return WL.Server.fetchNWBusinessObject(request);
}

1 个答案:

答案 0 :(得分:0)

回到你的客户端逻辑,在适配器调用的成功回调中,你得到一个JSON对象。此JSON对象包含响应。

所以我认为你应该做的是:

  1. 从响应和
  2. 中提取到另一个变量,或者直接使用“RETURN”键
  3. 再次使其成为可读的JSON格式,如下所示:http://davidwalsh.name/convert-xml-json
  4. 迭代刷新的respone以找到关键:您正在寻找的值。例如:

    for (i = 0; i < resultSet.length; i++) {
        if (resultSet[i].name == "some-value") { // assumed that you know what you're lookign for.
            alert (resultSet[i].name);
    }
    
  5. 第2步是困难的部分......不确定。