0

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.setEndpoint('https://dolewebservices.dole.com/soap?service=SalesforceDispatchIssues');
    request.setMethod('POST');
    request.setHeader('Content-Type', 'text/xml;charset=UTF-8');
    request.setHeader('Content-Length',String.valueOf(requestString.length()));
    //request.setHeader('SOAPAction', URL);
    // Set the body as a JSON object
    request.setBody(requestString);
    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">
   <soapenv:Body>
      <mesa:processData xmlns:mesa="http://www.sterlingcommerce.com/mesa">
         <ProcessData>
            <b2b-protocol>http</b2b-protocol>
            <transport-instance-id>HTTPServerAdapterLocalInboundnode2_HttpServerAdapter_node2</transport-instance-id>
            <transport-session-id>Sun Nov 05 18:40:55 PST 2017:4555</transport-session-id>
            <http-request-uri>/soap</http-request-uri>
            <http-method>POST</http-method>
            <service>SalesforceDispatchIssues</service>
            <SyncModeBP>false</SyncModeBP>
            <xport-doc-on-error>true</xport-doc-on-error>
            <incomingSOAPTransportMode>http</incomingSOAPTransportMode>
            <PipeLineTimeout>600</PipeLineTimeout>
            <messageMode>1</messageMode>
            <wsConfig name="SalesforceDispatchIssues">
               <certID/>
               <verificationCertID/>
            </wsConfig>
            <SOARequiredSignature>false</SOARequiredSignature>
            <EXPECT_SECURITY_HEADER>false</EXPECT_SECURITY_HEADER>
            <SOAP_URI>/soap</SOAP_URI>
            <SOAPEnvNSPrefix>soapenv</SOAPEnvNSPrefix>
            <SOAPEnvNSURI>http://schemas.xmlsoap.org/soap/envelope/</SOAPEnvNSURI>
            <soapenv:ReceivedSOAPHeaders/>
            <PrimaryDocument SCIObjectID="14736315fa612f807node2"/>
            <mesa:dole_prod_salesforcedispatch>
               <SalesOrderNumber>2107308</SalesOrderNumber>
            </mesa:dole_prod_salesforcedispatch>
            <serviceMode>0</serviceMode>
            <typeName>dole_prod_salesforcedispatch</typeName>
            <Result>
               <Row>
                  <so_num>2107308</so_num>
                  <cust_name>COSTCO #203  GLEN BURNIE</cust_name>
                  <hold_flag/>
                  <SalesManager>MMOYER</SalesManager>
                  <relatedorders/>
                  <products>CPO53C, GTG05C, HPO3RPI</products>
                  <container/>
                  <chassis/>
                  <genset/>
                  <yard_loc/>
                  <container_hold/>
                  <chassis_hold/>
                  <genset_hold/>
               </Row>
            </Result>
            <ADD_SOAP_ENVELOPING>false</ADD_SOAP_ENVELOPING>
            <INSERT_SECURITY_HEADER>false</INSERT_SECURITY_HEADER>
         </ProcessData>
      </mesa:processData>
   </soapenv:Body>
</soapenv:Envelope>

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 Nov 10 '17 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. – Shobhit Saxena Nov 11 '17 at 13:31
  • Let me post my code as well. Please refer to the code above. – Shobhit Saxena Nov 11 '17 at 13:32
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);
             while(m.find()){
                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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.