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
SFDC_LearnerSFDC_Learner 

XML parsing

Hi I have written an apex class to Parse XML String.

 

Page is :

 

<apex:page controller="XMLParseClass">
<apex:form >
<apex:pageblock >
<apex:pageblocksection >
<apex:inputfile value="{!objD.body}" filename="{!objD.name}"></apex:inputfile>
<apex:commandButton value="Parse" action="{!doParse}"/>
</apex:pageblocksection>

<apex:pageblocktable value="{!lstW}" var="w">
<apex:column headerValue="Emp Name" value="{!w.empName}"/>
<apex:column headerValue="Emp Id" value="{!w.empId}"/>
<apex:column headerValue="Emp City" value="{!w.empCity}"/>
</apex:pageblocktable>
</apex:pageblock>
</apex:form>
</apex:page>

 

 

class is :

 

 

public with sharing class XMLParseClass {

public Document objD {get;set;}

public void doParse(){

}

public List<Wrapper> lstW{get;set;}
public XMLParseClass(){
objD = new Document();
List<STring> lstvalues = new List<String>();
//String xml = objD.body.toString();
String xml ='<Company><Department><Employee><Name>A1</Name><EmpId>001</EmpId><city>Hyd</city></Employee><Employee><Name>A2</Name><EmpId>002</EmpId><city>Banglore</city></Employee><Employee><Name>A3</Name><EmpId>003</EmpId><city>Chennai</city></Employee></Department></Company>';
system.debug('--xml is -->'+xml);
Dom.Document doc = new Dom.Document();
doc.load(xml);

for(DOM.XMLNode rootnode : doc.getRootElement().getChildElements()){
for(DOM.XMLNode dept: rootnode.getChildElements()){

for(DOM.XMLNode Emp: dept.getChildElements()){
lstValues.add(Emp.getText()); // In this line, we can write the code to get the data and can build wrapper list. But i could not build this. Instead of this, i have written a code using for loop (as i selected below)
}
}
}
List<STring> names = new List<String>();
List<STring> Ids = new List<String>();
List<STring> Cities = new List<String>();
for(Integer i=0;i<lstValues.size();i+=3){
names.add(lstValues[i]);
}
for(Integer i=1;i<lstValues.size();i+=3){
Ids.add(lstValues[i]);
}
for(Integer i=2;i<lstValues.size();i+=3){
cities.add(lstValues[i]);
}
lstW = new List<wrapper>();
wrapper objW = new wrapper();
for(Integer i=0;i<Ids.size();i++){
objW = new wrapper();
objW.empName = names[i];
objW.empId = Ids[i];
objW.empCity = cities[i];
lstW.add(objW);
}
system.debug('---lstW is --->'+lstW);
}

public class wrapper{
public String empName{get;set;}
public String empId{get;set;}
public String empcity{get;set;}
}
}

 

 

 

Here in this class i feel that, we can optimize the code.

 

The Red colored code can be optimized and can easily build a wrapper list.

 

Can you help me to optimize the above code..

 

sfdcfoxsfdcfox
public with sharing class XMLParser {
    public static List<List<Map<String,String>>> parse() {
        String xml = '<Company><Department><Employee><Name>A1</Name><EmpId>001</EmpId><city>Hyd</city></Employee><Employee><Name>A2</Name><EmpId>002</EmpId><city>Banglore</city></Employee><Employee><Name>A3</Name><EmpId>003</EmpId><city>Chennai</city></Employee></Department></Company>';
        Dom.Document doc = new Dom.Document();
        doc.load(xml);
        List<List<Map<String,String>>> departments = new List<List<Map<String,String>>>();
        for(Dom.XmlNode dept:doc.getRootElement().getChildElements()) {
            List<Map<String,String>> employees = new List<Map<String,String>>();
            for(Dom.XmlNode emp:dept.getChildElements()) {
                Map<String,String> employee = new Map<String,String>();
                for(Dom.XmlNode empProp:emp.getChildElements()) {
                    employee.put(empProp.getName(),empProp.getText());
                }
                employees.add(employee);
            }
            departments.add(employees);
        }
        return departments;
    }
}

This is a test I created. I tried following your code, then realized that you had gone one element too deep. Factoring out the last level, I came up with the above code, which returns a list of departments, of which is a list of employees, each employee being represented here by a map containing three elements. You could alter employees.add(employee) to employees.add(new EmployeeWrapper(employee.get("EmpId"),employee.get("Name"),employee.get("city")) if you wanted an employee wrapper class, and you could do the same for the department. I used native elements only to show how to correctly parse the file.