Friday 11 April 2014

XML Parsing using DOM Parser in salesforce

Apex Class
  1. public class XMLParsing{
  2.  
  3.   Public void parseXML(String xmlfile)
  4.   {
  5.        DOM.Document xmlDOC = new DOM.Document();
  6.        xmlDOC.load(xmlfile);
  7.        DOM.XMLNode rootElement = xmlDOC.getRootElement();
  8.        System.Debug('$$$$ Root Element'+rootelement);
  9.        
  10.        for(DOM.XMLNode xmlNodeObj:xmlDOC.getRootElement().getChildElements()){
  11.        
  12.        System.Debug('$$$ Child Elements'+xmlNodeObj);
  13.        for(DOM.XMLNode xmlNodeObjChild:xmlNodeObj.getChildren())
  14.        {
  15.        System.Debug('$$$ Childrens'+xmlNodeObjChild.getChildren());
  16.        
  17.              if(xmlNodeObjChild.getName()=='FirstName')
  18.              System.Debug(xmlNodeObjChild.getText());
  19.              if(xmlNodeObjChild.getName()=='Email')
  20.              System.Debug(xmlNodeObjChild.getText());
  21.              if(xmlNodeObjChild.getName()=='Phone')
  22.              System.Debug(xmlNodeObjChild.getText());
  23.              if(xmlNodeObjChild.getName()=='ReasonforContactString')
  24.              System.Debug(xmlNodeObjChild.getText());
  25.          
  26.             }
  27.              
  28.        }
  29.   }
  30.  
  31.  
  32. }

Test Class

  1. @isTest
  2. public class TestXMLParser
  3. {
  4.   static testMethod void testXmlParser() {
  5.  
  6.     XMLParsing a=new XMLParsing();
  7.     String str = '<AptiLeads><lead><FirstName>Arunkumar</FirstName><LastName>R</LastName><Email>arun@gmail..com</Email><Phone>7777777777</Phone><ReasonforContact>Associate of Arts in Business</ReasonforContact></lead><lead><FirstName>Balaji</FirstName><LastName>Ram</LastName><Email>ram@gmail.com</Email><Phone>999999991</Phone><ReasonforContact>Associate of Arts in Busines1s</ReasonforContact></lead></AptiLeads>';
  8.     a.parseXML(str);
  9.  
  10.    }
  11. }

1 comment:

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...