使用SAPUI5和SOAP请求以及来自Web服务的响应

时间:2018-06-06 16:57:01

标签: javascript soap xml-parsing sapui5

我的问题是我正在开发一个在flash中制作的重构项目,并且必须将其转换为SAUI5并且为此我使用了肥皂网络服务。它有多个Web服务部分。 Ajax调用如下所示:

`var oAppSettings = sap.ui.getCore().getModel("appSettings").getData();
        var response;
        var oData;
        var oXMLModel = new sap.ui.model.xml.XMLModel();

        var sReq = "<soapenv:Envelope 
        xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" 
        xmlns:web=\"http://webservice.cpb.dqw.sap.com\">\n" +
            "   <soapenv:Header/>\n" +
            "   <soapenv:Body>\n" +
            "      <web:boeLogonWithToken>\n" +
            "         <!--Optional:-->\n" +
            "         <web:args0>"+oAppSettings.loginToken+"</web:args0>\n" 
            +
            "      </web:boeLogonWithToken>\n" +
            "   </soapenv:Body>\n" +
            "</soapenv:Envelope>";

        $.ajax({
            url: oAppSettings.serverPath + ".AdminHttpSoap11Endpoint/",
            method: "POST",
            dataType: "xml",
            data: sReq,
            //processData:false,
            contentType: "text/xml; charset=\"utf-8\"",
            success: function (data, textStatus, jqXHR) {
                response = data;
                console.log(response);
                console.log("Is a success!");
            },
            error: function (xhr, status) {
                console.log("Error: : " + status);
            },
            complete: function (xhr, status) {
                console.log(response);
                setUpData();
            }
        });

        function setUpData(){
            oXMLModel.setData(response);

            console.log(oXMLModel.getXML());
        }`

我得到的回应是:

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns:boeLogonWithTokenResponse 
         xmlns:ns="http://webservice.cpb.dqw.sap.com">
            <ns:return xmlns:ax22="http://shared.cpb.dqw.sap.com/xsd" 
             xmlns:ax21="http://types.cpb.dqw.sap.com/xsd" 
             xmlns:ax24="http://types.sdk.boe.dqw.sap.com/xsd" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:type="ax21:CPBAdminResult">
                <ax21:contentUpgradeVersion>0</ax21:contentUpgradeVersion>
                <ax21:cpInfo xsi:nil="true" />
                <ax21:errorData xsi:nil="true" />
                <ax21:intValue xsi:nil="true" />
                <ax21:projectInfo xsi:nil="true" />
                <ax21:reservedData xsi:nil="true" />
                <ax21:status>OK</ax21:status>
                <ax21:stringArray xsi:nil="true" />
                <ax21:stringValue xsi:nil="true" />
            </ns:return>
        </ns:boeLogonWithTokenResponse>
    </soapenv:Body>
</soapenv:Envelope>`

我想知道如何解析通过SAPUI5的xml模型返回的xml。

谢谢

2 个答案:

答案 0 :(得分:0)

您可以使用:( jQuery将与SAPUI5一​​起加载)

var xmlContent =
    `<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns:boeLogonWithTokenResponse 
         xmlns:ns="http://webservice.cpb.dqw.sap.com">
            <ns:return xmlns:ax22="http://shared.cpb.dqw.sap.com/xsd" 
             xmlns:ax21="http://types.cpb.dqw.sap.com/xsd" 
             xmlns:ax24="http://types.sdk.boe.dqw.sap.com/xsd" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:type="ax21:CPBAdminResult">
                <ax21:contentUpgradeVersion>0</ax21:contentUpgradeVersion>
                <ax21:cpInfo xsi:nil="true" />
                <ax21:errorData xsi:nil="true" />
                <ax21:intValue xsi:nil="true" />
                <ax21:projectInfo xsi:nil="true" />
                <ax21:reservedData xsi:nil="true" />
                <ax21:status>OK</ax21:status>
                <ax21:stringArray xsi:nil="true" />
                <ax21:stringValue xsi:nil="true" />
            </ns:return>
        </ns:boeLogonWithTokenResponse>
    </soapenv:Body>
</soapenv:Envelope>`;
var res = jQuery.parseXML(xmlContent)
var values = res.getElementsByTagName("ns:return")[0];
var result = {};
for (var i = 0; i < values.children.length; i++) {
    var key = values.children[i].nodeName.replace("ax21:", "");
    result[key] = values.children[i].innerHTML;
}
console.log(result);

请注意,这有点粗鲁。

您可以将其转换为通用版。

我的建议是从服务器获取JSON本身,而​​不是在UI上执行此马戏团。

干杯!

答案 1 :(得分:0)

您的问题似乎想知道如何使用XMLModel(并且您应该坚持这个答案;))因为它比首次出现更简单,并且意味着您不必转换为JSON。

使用以下提供的XML创建XMLModel的实例:

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users(businessPhones,displayName,givenName,jobTitle,mail,mobilePhone,officeLocation,preferredLanguage,surname,userPrincipalName,id,mailNickname)/$entity",
    "id": "48d31887-5fad-4d73-a9f5-3c356e68a038",
    "businessPhones": [
        "+1 412 555 0109"
    ],
    "displayName": "Megan Bowen",
    "givenName": "Megan",
    "jobTitle": "Auditor",
    "mail": "MeganB@M365x214355.onmicrosoft.com",
    "mailNickname": "MeganB",
    "mobilePhone": null,
    "officeLocation": "12/1110",
    "preferredLanguage": "en-US",
    "surname": "Bowen",
    "userPrincipalName": "MeganB@M365x214355.onmicrosoft.com"
}

导航到您要访问的元素:

var oModel = new XMLModel();
oModel.loadData("response.xml");