简体   繁体   English

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

[英]Working with SAPUI5 and SOAP request and response from web service

My question is that I am working on a refactoring project that was made in flash and have to convert it to SAUI5 and to do that I have use a soap web service. 我的问题是,我正在开发一个用Flash制作的重构项目,必须将其转换为SAUI5,并且要使用肥皂网络服务来完成。 It has multiple parts to the web service. 它包含Web服务的多个部分。 Ajax call looks like this: 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());
        }`

the response I get is this: 我得到的答复是这样的:

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

I would like to know how to parse through the xml returned with the xml model of SAPUI5. 我想知道如何解析通过SAPUI5的xml模型返回的xml。

Thank you 谢谢

You can use: (jQuery will be loaded already with SAPUI5) 您可以使用:(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);

Please note that this is bit crude way. 请注意,这有点粗糙。

You can convert this into generic one. 您可以将其转换为通用的。

My suggestion would be get JSON itself from the server instead of doing this circus on UI. 我的建议是从服务器本身获取JSON,而不是在UI上进行此操作。

Cheers! 干杯!

Your question seemed to want to know how to use the XMLModel (and you should have held out for that answer ;)) as it is simpler than it first appears and means you do not have to convert to JSON. 您的问题似乎想知道如何使用XMLModel(并且您应该为该答案提供支持;),因为它比第一次出现时要简单,并且意味着您不必转换为JSON。

Create an instance of your XMLModel with the XML supplied in: 使用提供的XML创建XMLModel的实例:

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

Navigate to the element you want to get to: 导航到要获取的元素:

var path = "/soapenv:Body/ns:boeLogonWithTokenResponse/ns:return";
oModel.attachRequestCompleted(function(){
        var status = oModel.getProperty(path + "/ax21:status"); 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM