【问题标题】:Working with SAPUI5 and SOAP request and response from web service使用来自 Web 服务的 SAPUI5 和 SOAP 请求和响应
【发布时间】:2025-11-25 22:15:01
【问题描述】:

我的问题是,我正在处理一个在 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进行解析。

谢谢

【问题讨论】:

  • 您是在问如何只解析结果吗?没有任何 XML 解析器能够提供这个吗?
  • 我一直在尝试一切,当我尝试最简单的解析器示例时,它返回未定义。不久前尝试过这个:var parser, text, xmlDoc;解析器 = 新的 DOMParser(); xmlDoc = parser.parseFromString(response, "text/xml"); console.log(xmlDoc.getElementsByTagName("ns\\:return")[0].childNodes[6].nodeValue);
  • 您应该在问题的文本中显示您尝试过的内容以及任何错误消息。请参阅How to Ask

标签: javascript soap xml-parsing sapui5


【解决方案1】:

您可以使用:(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 上执行此操作。

干杯!

【讨论】:

  • 谢谢你,我会试一试,因为我这样做是在尽可能不改变网络服务的情况下完成的。
  • 附带说明,如果其中一个标签有多个值,那么我会添加一个额外的循环来获取每个标签。
【解决方案2】:

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

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

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

导航到您要访问的元素:

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

【讨论】:

    最近更新 更多