Tuesday 24 December 2013

Parsing XML file in Apex Class

Somedays ago I was in need to parse the XML response, Which I was getting in reponse. it require a bit labour to get a particular value from that XML file.

In salesforce most of the time when we are working with webservice, we need to parse JSON or XML. So here I am doing a small POC of parsing XML file in salesforce.
Here am trying to fetch the value of "R10TnxID" from the file.

So below is the code, for more detail you can visit on This Link
Here you need learn a bit about DOM

/** Description : Parse XML file. String Response is the XML file which is going to be parsed.
*
* Created By : Abhi Tripathi 
*
* Created Date : 07/09/2013
*
* Revisison Log : 07/09/2013 
*
* Version : V1.0
**/
public with sharing class domXmlParsing {
    
    //method to parse xml file 
    public static string walkThrough(){
        
        //XML string to parse
        String response = '<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">'
            + '<S:Body>'
            + '<wss:createPendingTicketResponse xmlns:wss="http://www.boomi.com/connector/wss">'
            + '<wss:createPaymentTransctionLogResponse/>'
            + '<PendingTicket xmlns:pay="http://www.boomi.com/v1b/Payment">'
            + '<pay:Response>'
            + '<pay:R10TillID>test001</pay:R10TillID>'
            + '<pay:R10SequenceNumber>test002</pay:R10SequenceNumber>'
            + '<pay:SFTnxId>test003</pay:SFTnxId>'
            + '<pay:R10TnxID>'+ 'TESTME' +'</pay:R10TnxID>'
            + '<pay:Org>test004</pay:Org>'
            + '</pay:Response>'
            + '</PendingTicket>'
            + '</wss:createPendingTicketResponse>'
            + '</S:Body>'
            + '</S:Envelope>';
        
        //dom
        Dom.Document doc = new Dom.Document();
        
        //string
        string result;
        
        //load document
        doc.load(response);
        
        Dom.XMLNode envelope = doc.getRootElement();
        system.debug('!!!!!!!!! value of envelope' + envelope);
        
        //body
        Dom.XMLNode body = envelope.getChildElement('Body', 'http://schemas.xmlsoap.org/soap/envelope/'); 
        system.debug('@@@@@ value of body' + body);
        
        //child element of createPendingTicketResponse
        Dom.XMLNode createPendingTicketResponse = body.getChildElement('createPendingTicketResponse', 'http://www.boomi.com/connector/wss'); 
        system.debug('@@@@@ value of createPendingTicketResponse' + createPendingTicketResponse);
        
        //child element of createPaymentTransctionLogResponse
        Dom.XMLNode createPaymentTransctionLogResponse = createPendingTicketResponse.getChildElement('createPaymentTransctionLogResponse', 'http://www.boomi.com/connector/wss'); 
        system.debug('@@@####@@ value of createPaymentTransctionLogResponse' + createPaymentTransctionLogResponse);
        
        //child element of pending Ticket
        Dom.XMLNode PendingTicket = createPendingTicketResponse.getChildElement('PendingTicket', null); 
        system.debug('@@@####@@ value of PendingTicket' + PendingTicket);
        
        //child element of Response 
        Dom.XMLNode Respon = PendingTicket.getChildElement('Response', 'http://www.boomi.com/v1b/Payment'); 
        system.debug('@@@####@@ value of Response' + Respon); 
        
        //child element of R10TnxID
        Dom.XMLNode R10TnxID = Respon.getChildElement('R10TnxID', 'http://www.boomi.com/v1b/Payment'); 
        system.debug('@@@####@@ value of R10TnxID' + R10TnxID); 
        
        //here is the value in R10TnxID
        result = R10TnxID.getText();
        system.debug('@@@####@@ value of result' + result); 
        
        return result;
    }
}


After saving this above class, go to your Developer Console and write as below shown and then check your debug logs, you'll get the value of R10TnxID


Happy Coding....!!!!!!

No comments:

Post a Comment