I had called an endpoint using HTTP Post request which has sent the response in the form of an XML, but it is not containing the usual start and ending tags. Here is how I called this request from a simple HTTP get request, instead of calling the WSDL generated Apex, since it was throwing exception everytime due to tags being used in the WSDL file.

        String requestString = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mesa="http://www.sterlingcommerce.com/mesa"><soapenv:Header/><soapenv:Body><mesa:dole_prod_salesforcedispatch xmlns:mesa="http://www.sterlingcommerce.com/mesa"><SalesOrderNumber>2107308</SalesOrderNumber><!--You may enter ANY elements at this point--></mesa:dole_prod_salesforcedispatch></soapenv:Body></soapenv:Envelope>';
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setHeader('Content-Type', 'text/xml;charset=UTF-8');
    //request.setHeader('SOAPAction', URL);
    // Set the body as a JSON object
    HttpResponse response = http.send(request);
    // Parse the JSON response
    if (response.getStatusCode() != 200) {
        System.debug('The status code returned was not expected: ' +
            response.getStatusCode() + ' ' + response.getStatus());
    } else {
        System.debug('Response received'+response.getBody());

I am in need of fetching the information returned in the result to populate them in sObject's fields. How can I parse this sort of a response?

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <mesa:processData xmlns:mesa="http://www.sterlingcommerce.com/mesa">
            <transport-session-id>Sun Nov 05 18:40:55 PST 2017:4555</transport-session-id>
            <wsConfig name="SalesforceDispatchIssues">
            <PrimaryDocument SCIObjectID="14736315fa612f807node2"/>
                  <cust_name>COSTCO #203  GLEN BURNIE</cust_name>
                  <products>CPO53C, GTG05C, HPO3RPI</products>

I tried the same using the DOM.Document class, but not sure whether it is the specific solution to my requirement.

  • Probably the easiest way to handle this would be to download a WSDL file for SterlingCommerce (if they provide one), and use WSDL2Apex to generate appropriate classes to handle requests/responses from that service.
    – Derek F
    Commented Nov 10, 2017 at 15:57
  • Hi @Derek F, I did the same, but the Apex Class generated from WSDL is of no use, since it is not able to send request to the web service. I've dedicated my weeks of time in trying to do so. The reason being <any> used in the WSDL. So, later I tried sending a simple HTTP request by passing the entire request of xml, and received the desired response. Not sure how to parse useful information from the response, though. Commented Nov 11, 2017 at 13:31
  • Let me post my code as well. Please refer to the code above. Commented Nov 11, 2017 at 13:32

1 Answer 1


You can read the xml by Dom.Document

Ref: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_xml_dom.htm

Sometimes if you are getting only single entriy of data then you can use below method to get data between xml tags.

   public static String getDataBetweenTags(String tagName, String str){
             Matcher m = Pattern.compile('(<'+tagName+'>)([\\s\\S]*?)(</'+tagName+'>)').matcher(str);
                String result = str.substring(m.start(), m.end());
                result = result.replaceAll('<'+tagName+'>','');
                result = result.replaceAll('</'+tagName+'>','');
                return result;
             return '';      

Good way to use is Dom.Document for reading xml.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .