function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ramakrishnan AyyanarRamakrishnan Ayyanar 

Parsing An XML Data With DOM Parser In Apex

XmlParseVFPage:

<apex:page Controller="Xmlparsar">

<apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Parse Xml" action="{!Parsexml}" />   
                <apex:commandButton value="ParseXML File" action="{!Parsexmlfile}"/>
            </apex:pageBlockButtons>
            <apex:inputTextArea value="{!xmlstring}" style="width:336px;height:260px;"/> &nbsp;&nbsp;
            <apex:inputTextArea value="{!outxmlstring}" style="width:336px;height:260px;" id="response"/><br/>

            <apex:outputLabel value="Select Xml File" for="file"/>
            <apex:inputFile fileName="{!fileName}" value="{!body}"/>
        </apex:pageBlock>
    </apex:form>
   
</apex:page>

Apex Class

public  class Xmlparsar
{
    //xml string
    public String xmlstring{get;set;}
   
    //display xml string
    public String outxmlstring{get;set;}
   
    //rootelement
    public String rootElement{get;set;}
   
    //
    public String filename{get;set;}
   
    public blob body{get;set;}
      
    //constructor
    public Xmlparsar()
    {
    
    }
   
    
//Parsing xml what you entered in the left text area
    public pagereference Parsexml()
    {
       DOM.Document xmlDOC = new DOM.Document();
       xmlDOC.load(xmlstring);
       DOM.XMLNode rootElement = xmlDOC.getRootElement();
       outxmlstring=String.valueof(xmlDOC.getRootElement().getName());
       for(DOM.XMLNode xmlnodeobj:xmlDOC.getRootElement().getChildElements())
       //.getChildren())
       {        
         
         
          loadChilds(xmlnodeobj);         
       }      
       return null;
    }
   
    //loading the child elements
    public void loadChilds(DOM.XMLNode xmlnode)
    {
        for(Dom.XMLNode child : xmlnode.getChildElements())
        {
          if(child.getText()!= null)
          {
          outxmlstring+='\n'+child.getName()+': '+child.getText();
       
          }
          loadChilds(child);       
        }
    }
   
   
//This is for parsing xml file what you selected
  public pagereference Parsexmlfile()
  {
       DOM.Document xmlDOC = new DOM.Document();
       xmlstring=body.tostring();         
       xmlDOC.load(xmlstring);
       DOM.XMLNode rootElement = xmlDOC.getRootElement();
       outxmlstring=String.valueof(xmlDOC.getRootElement().getName());//gives you root element Name
       for(DOM.XMLNode xmlnodeobj:xmlDOC.getRootElement().getChildElements())
       {        
                  
          loadChilds(xmlnodeobj); 
       }      
      return null;
    }
}

Apex Test Class

@isTest
public class XmlParsarTest
{
    
    //test for parse xml only
    @isTest static void Parsexml()
    {      
        Xmlparsar xmlpar=new Xmlparsar();
        PageReference pageRef = Page.XmlParseVFPage;
        Test.setCurrentPage(pageRef);
        xmlpar.xmlstring='<?xml version="1.0"?><catalog><book id="bk101"><author>Gambardella, Matthew</author></book></catalog>';
        xmlpar.Parsexml();
        System.assertNotEquals(null, xmlpar.outxmlstring);
    }
   
    @isTest static void ParsexmlFile()
    {
        Xmlparsar xmlpar=new Xmlparsar();
        PageReference pageRef = Page.XmlParseVFPage;
        Test.setCurrentPage(pageRef);
        List<StaticResource> resourceList=[SELECT body FROM StaticResource WHERE Name = :'testxmlfile' limit 1];
        xmlpar.body=resourceList[0].body;
        System.debug('xmlpar.body'+xmlpar.body);
        System.debug('xmlpar.xmlstring'+xmlpar.xmlstring);
        xmlpar.Parsexmlfile();
        System.assertNotEquals(null, xmlpar.outxmlstring);
    }
   
  
}
Vinita_SFDCVinita_SFDC
Hi,

This a good code sample for XML parsing with DOM parser. I would suggets you add a summary in two lines about what this code is about, as initially i was looking for what i need to answer here :)
Ramakrishnan AyyanarRamakrishnan Ayyanar
Vinita_SFDC@

My Code had two type of parsing method here.

1.Parse Xml
2.Parse Xml File

Parse Xml--->Dircectly parse xml file ,using Parsexml method used. Check Root Element have child.If It's had child call loadChilds method. 
Parse Xml File-->Choose Xml file (xml file)blob to string,using Parsexmlfile method used.Check Root Element have child.If It's had child call loadChilds method.



Thanks,
Ramakrishnan Ayyanar,
+919944112175
softramki@gmail.com
DesaiDesai
Hi Ramakrishan,

How do we get each child elements value and pass to a field instead of passign the entire parsed data ?

Thanks