Search code examples
javascriptsoapxml-parsingsapui5

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. It has multiple parts to the web service. Ajax call looks like this:

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

Thank you


Solution

  • You can use: (jQuery will be loaded already with 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.

    Cheers!