Ash Hogarth
Ash Hogarth

Reputation: 567

Extracting Data From SOAP with Javascript

I have a SOAP response from a web service and I'm just trying to extract the 'Amount' element from the 'Total Cost' within the body element. I am familiar with how to do this in JSON but I'm struggling to find any material online about how to do it with SOAP using Javascript/jQuery.

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns1:TotalCostResponse xmlns="http://get_total_cost_request.business.ws.test.com" xmlns:ns1="http://business.ws.test.com" xmlns:ns2="http://get_total_cost_response.business.ws.test.com">
         <ns2:MSG>
            <ns2:HEADER>
               <ns2:SITENO>1</ns2:SITENO>
               <ns2:SYSTEM_ID>S01</ns2:SYSTEM_ID>
               <ns2:CLUSTER_NAME>BUSINESS</ns2:CLUSTER_NAME>
               <ns2:SERVICE_METHOD>TOTAL_COST</ns2:SERVICE_METHOD>
               <ns2:VERSION_NO>1.0</ns2:VERSION_NO>
            </ns2:HEADER>
            <ns2:BODY>
               <ns2:TOTAL_COST>
                  <ns2:AMOUNT>2139.82</ns2:AMOUNT>
                  <ns2:CURRENCY>GBP</ns2:CURRENCY>
               </ns2:TOTAL_PREMIUM>
            </ns2:BODY>
         </ns2:MSG>
      </ns1:TotalCostResponse>
   </soap:Body>
</soap:Envelope>

In JSON this would be something like;

var obj = JSON.parse(data.TotalCostResponse.MSG.BODY.TOTALCOST.AMOUNT);

but for the life of me I cant work out how to do it with SOAP. Any help will be massively appreciated!

Update 1 The call is being made with jquery's AJAX method, according to the console MSG is undefined. Could this be to do with SOAP namespacing?

$.ajax({
 type: "POST",
 url: URL,
 data: dataSet,
 dataType: "xml",
 contentType: "text/xml; charset=\"utf-8\"",
 success: function(data) {

  obj = $.parseXML(data.TotalCostResponse.MSG.BODY.TOTAL_COST.AMOUNT);

        }});

Upvotes: 0

Views: 1702

Answers (1)

seenukarthi
seenukarthi

Reputation: 8664

XML cannot be parsed using JSON.parse, it is for parsing JSON string. You can use jQuery.parseXML()

var obj = $.parseXML(data.TotalCostResponse.MSG.BODY.TOTALCOST.AMOUNT);

Upvotes: 1

Related Questions