Monday, 3 June 2013

XML Parsing

I am going to give a brief explanation on xml parsing.we have DOM parser to parse xml.
Before we are going to parse an xml file we need to look its strcture and we should expect how many we may need for parsing the "XML" which have to avoid number of  script statemements limitation in Apex.
Use the XmlNode class to work with a node in an XML document. The DOM represents an XML document as a hierarchy of nodes. Some nodes may be branch nodes and have child nodes, while others are leaf nodes with no children.
Node Types
There are different types of DOM nodes available in Apex. XmlNodeType is an enum of these different types. The values are:
It is important to distinguish between elements and nodes in an XML document. The following is a simple XML example:
This example contains three XML elements: name, firstName, and lastName. It contains five nodes: the three name, firstName, and lastNameelement nodes, as well as two text nodes—Balaji and Marpuram. Note that the text within an element node is considered to be a separate text node.
public  class Xmlparsercls {
    public String xmlstring{get;set;}
    public String outxmlstring{get;set;}
    public String rootElement{get;set;}
    public String filename{get;set;}
    public blob body{get;set;}
    public List<Account> lstaccount{get;set;}
    public Xmlparsercls(){
      lstaccount=new List<Account>();
//Parsing xml what you entered in the left text area
    public pagereference Parsexml(){
       DOM.Document xmlDOC = new DOM.Document(); 
       DOM.XMLNode rootElement = xmlDOC.getRootElement();
       for(DOM.XMLNode xmlnodeobj:xmlDOC.getRootElement().getChildElements()){
            outxmlstring+='\nAccount Name:'+xmlnodeobj.getText();
            outxmlstring+='\nAccount Type:'+xmlnodeobj.getText();  
            outxmlstring+='\nAccount Industry:'+xmlnodeobj.getText();
      return null;
//This is for parsing xml file what you selected
  public pagereference Parsexmlfile(){
       DOM.Document xmlDOC = new DOM.Document(); 
      // xmlstring=xmlstring.Substring(1,xmlstring.length());
       DOM.XMLNode rootElement = xmlDOC.getRootElement();
       outxmlstring=String.valueof(xmlDOC.getRootElement().getName());//gives you root element Name
       for(DOM.XMLNode xmlnodeobj:xmlDOC.getRootElement().getChildElements()){ //gives you all childnodes list
            outxmlstring+='\nAccount Name:'+xmlnodeobj.getText();// it gives you text node
            outxmlstring+='\nAccount Type:'+xmlnodeobj.getText();  
            outxmlstring+='\nAccount Industry:'+xmlnodeobj.getText();
      return null;
<apex:page controller="Xmlparsercls" sidebar="false">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Parse Xml" action="{!Parsexml}"/>    
                <apex:commandButton value="ParseXML File" action="{!Parsexmlfile}"/>
            <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}"/>

Here i am choosing XML format in Text area or in xml file as shown below .According to the file we need to write and parse.

XML format:

when you work with this example and want to test with xml file copy the same xml format as above into your notepad and save with ".xml" extension.

When you are selecting file then click on ParseXML file button.
When you type as shown above in the textarea then click on "Parse Xml" button then you can parse the xml.