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
Veerendar AellaVeerendar Aella 

how to import xml data file into lead records using apex and visualforce page??

Hi All,

I want to import an xml file into my lead records using the apex code and visualforce page on a single click from the page.
here is my sample xml file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<lead-data >
    <record>
        <FirstName>Veera</FirstName>
        <LastName>Vinnu</LastName>
        <Phone>8008997654</Phone>
        <Email>veerav@gmail.com</Email>
    </record>
    <record>
        <FirstName>Beera</FirstName>
        <LastName>Binnu</LastName>
        <Phone>9898989898</Phone>
        <Email>beeru@gmail.com</Email>
    </record>
    <record>
        <FirstName>Ceera</FirstName>
        <LastName>Cinnu</LastName>
        <Phone>8989898989</Phone>
        <Email>ceera@gmail.com</Email>
    </record>
    <record>
        <FirstName>Deera</FirstName>
        <LastName>Dinnu</LastName>
        <Phone>7878787878</Phone>
        <Email>deera@gmail.com</Email>
    </record>
</lead-data>


Please help me out.
Best Answer chosen by Veerendar Aella
Raj VakatiRaj Vakati
Use this code
 
<apex:page Controller="Parser_New" >
        
        <apex:form>        
            <apex:pageblock title="Read XML" id="PB">
                <!-- inputFile for uploading XML -->
                <apex:pageblocksection >
                    <apex:pageblocksectionitem>
                        <apex:outputLabel value="Please Select XML File:"/>  
                        <apex:inputFile value="{!myfile}"> </apex:inputFile>
                    </apex:pageblocksectionitem>                
                </apex:pageblocksection>
                <!-- Table to show the XML Result -->
                <apex:pageblocksection title="Result of XML" columns="1" rendered="{!reports.size != null}">
                    <apex:pageblocktable value="{!reports}" var="con">
                        <apex:column value="{!con.FirstName}" headerValue="First Name"/>
                        
                    </apex:pageblocktable>
                </apex:pageblocksection>
                <!-- Button for calling method of controller -->
                <center>
                    <apex:commandButton value="Read" action="{!doUpload}"/>
                </center>
            </apex:pageblock>    
        </apex:form>    
        
    </apex:page>
 
public class Parser_New{
    public Blob myfile{get;set;}
    
    
    public Parser_New(){
        reports = new List<leaddata>(); 
    }
    
    public List<leaddata> reports {get;set;}
    
    public class leaddata {
        public String FirstName {get; set;}
        public String LastName {get; set;}
        public String Phone {get; set;}
        public String Email {get; set;}
    }
    
    
    
    private void parseReports(DOM.XMLNode node) {
        for (Dom.XMLNode child : node.getChildElements()) {
            if (child.getName() == 'record') {
                System.debug('child'+child);
                parseReport(child);
                //  reports.add(r);
            }
            System.debug('reports'+reports);
        }
    }
    
    private void parseReport(DOM.XMLNode node ) {
        leaddata r = new leaddata();
        
        for (Dom.XMLNode child : node.getChildElements()) {
            if (child.getName() == 'FirstName') {
                r.FirstName= child.getText().trim();
            } else if (child.getName() == 'LastName') {
                r.Lastname= child.getText().trim();
            } else if (child.getName() == 'Phone') {
                r.Phone= child.getText().trim();
            }  else if (child.getName() == 'Email') {
                r.Email= child.getText().trim();
                
            }
        }
        reports.add(r);
        
    }
    
    public void doUpload() {
        
        DOM.Document doc = new DOM.Document();
        doc.load(String.valueOf(myfile.toString()));    
        parseReports(doc.getRootElement());
        
        
    }
    
}

 

All Answers

Raj VakatiRaj Vakati
Use this code
 
<apex:page Controller="Parser_New" >
        
        <apex:form>        
            <apex:pageblock title="Read XML" id="PB">
                <!-- inputFile for uploading XML -->
                <apex:pageblocksection >
                    <apex:pageblocksectionitem>
                        <apex:outputLabel value="Please Select XML File:"/>  
                        <apex:inputFile value="{!myfile}"> </apex:inputFile>
                    </apex:pageblocksectionitem>                
                </apex:pageblocksection>
                <!-- Table to show the XML Result -->
                <apex:pageblocksection title="Result of XML" columns="1" rendered="{!reports.size != null}">
                    <apex:pageblocktable value="{!reports}" var="con">
                        <apex:column value="{!con.FirstName}" headerValue="First Name"/>
                        
                    </apex:pageblocktable>
                </apex:pageblocksection>
                <!-- Button for calling method of controller -->
                <center>
                    <apex:commandButton value="Read" action="{!doUpload}"/>
                </center>
            </apex:pageblock>    
        </apex:form>    
        
    </apex:page>
 
public class Parser_New{
    public Blob myfile{get;set;}
    
    
    public Parser_New(){
        reports = new List<leaddata>(); 
    }
    
    public List<leaddata> reports {get;set;}
    
    public class leaddata {
        public String FirstName {get; set;}
        public String LastName {get; set;}
        public String Phone {get; set;}
        public String Email {get; set;}
    }
    
    
    
    private void parseReports(DOM.XMLNode node) {
        for (Dom.XMLNode child : node.getChildElements()) {
            if (child.getName() == 'record') {
                System.debug('child'+child);
                parseReport(child);
                //  reports.add(r);
            }
            System.debug('reports'+reports);
        }
    }
    
    private void parseReport(DOM.XMLNode node ) {
        leaddata r = new leaddata();
        
        for (Dom.XMLNode child : node.getChildElements()) {
            if (child.getName() == 'FirstName') {
                r.FirstName= child.getText().trim();
            } else if (child.getName() == 'LastName') {
                r.Lastname= child.getText().trim();
            } else if (child.getName() == 'Phone') {
                r.Phone= child.getText().trim();
            }  else if (child.getName() == 'Email') {
                r.Email= child.getText().trim();
                
            }
        }
        reports.add(r);
        
    }
    
    public void doUpload() {
        
        DOM.Document doc = new DOM.Document();
        doc.load(String.valueOf(myfile.toString()));    
        parseReports(doc.getRootElement());
        
        
    }
    
}

 
This was selected as the best answer
Veerendar AellaVeerendar Aella
Hi Raj,

Thank you so much, what if want to insert these into my lead object?

as of now i only can read the xml file
Veerendar AellaVeerendar Aella
Thanks once again raj, it worked fine
Raj VakatiRaj Vakati
Great ! Close other thread also