Skip to content Skip to sidebar Skip to footer

How To Parse Xml File On Android

Does android has some dom/sax parsers? E.g. of xml file: My file is more complex but i this was a simp

Solution 1:

Yes, Android also supports SAXParser, and the code will not differ from a normal java program.

SAX parser use callback function (org.xml.sax.helpers.DefaultHandler) to informs clients of the XML document structure. You should extend DefaultHandler and override few methods to achieve xml parsing. The methods to override are

  • startDocument() and endDocument() – Method called at the start and end of an XML document.
  • startElement() and endElement() – Method called at the start and end of a document element.
  • characters() – Method called with the text contents in between the start and end tags of an XML document element.

For a working code visit: XML parsing using SaxParser with complete code

Solution 2:

[Android Xml Parsing Tutorial][1]

         we can parse xml files in android is very simple. In market there are many xml parsers are available to parse xml datain android. But simplexml is one of the best xml parser in android.
  1. Create one Android Application Project.
  2. Download simple-xml.jar from internet.
  3. Prepare your xml file.

Employee.xml

<Employees><Employee><id>01</id><name>jagadeesh</name><salary>00000</salary></Employee><Employee><id>02</id><name>jaggubai</name><salary>00000</salary></Employee><Employee><id>03</id><name>jaggudada</name><salary>00000</salary></Employee><Employees>
  1. Now you need to create two pojo classes. Because here we are using two things one is group of employees and another is individual employees. So, for group of employees we need to create Employees.java and for individual employees Employee.java.

Employee.java

publicclassEmployee{
    @ElementpublicString id;
    @ElementpublicString name;
    @ElementpublicString salary;
}

Employees.java

@RootpublicclassEmployees{
    @ElementList(inline=true, entry="Employee")
    publicList<Employee> listOfEmployees;
}

Solution 3:

publicclassSAXParserExampleextendsDefaultHandler{

List myEmpls;

private String tempVal;

//to maintain contextprivate Employee tempEmp;


publicSAXParserExample(){
    myEmpls = new ArrayList();
}

publicvoidrunExample() {
    parseDocument();
    printData();
}

privatevoidparseDocument() {

    //get a factory
    SAXParserFactory spf = SAXParserFactory.newInstance();
    try {

        //get a new instance of parser
        SAXParser sp = spf.newSAXParser();

        //parse the file and also register this class for call backs
        sp.parse("employees.xml", this);

    }catch(SAXException se) {
        se.printStackTrace();
    }catch(ParserConfigurationException pce) {
        pce.printStackTrace();
    }catch (IOException ie) {
        ie.printStackTrace();
    }
}

/**
 * Iterate through the list and print
 * the contents
 */privatevoidprintData(){

    System.out.println("No of Employees '" + myEmpls.size() + "'.");

    Iterator it = myEmpls.iterator();
    while(it.hasNext()) {
        System.out.println(it.next().toString());
    }
}


//Event HandlerspublicvoidstartElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    //reset
    tempVal = "";
    if(qName.equalsIgnoreCase("Employee")) {
        //create a new instance of employee
        tempEmp = new Employee();
        tempEmp.setType(attributes.getValue("type"));
    }
}


publicvoidcharacters(char[] ch, int start, int length) throws SAXException {
    tempVal = new String(ch,start,length);
}

publicvoidendElement(String uri, String localName, String qName) throws SAXException {

    if(qName.equalsIgnoreCase("Employee")) {
        //add it to the list
        myEmpls.add(tempEmp);

    }elseif (qName.equalsIgnoreCase("Name")) {
        tempEmp.setName(tempVal);
    }elseif (qName.equalsIgnoreCase("Id")) {
        tempEmp.setId(Integer.parseInt(tempVal));
    }elseif (qName.equalsIgnoreCase("Age")) {
        tempEmp.setAge(Integer.parseInt(tempVal));
    }

}

publicstaticvoidmain(String[] args){
    SAXParserExample spe = new SAXParserExample();
    spe.runExample();
}

}

for xml <Personnel> <Employee type="permanent"> <Name>Seagull</Name> <Id>3674</Id> <Age>34</Age> </Employee> <Employee type="contract"> <Name>Robin</Name> <Id>3675</Id> <Age>25</Age> </Employee> <Employee type="permanent"> <Name>Crow</Name> <Id>3676</Id> <Age>28</Age> </Employee> </Personnel>

Check this link : http://www.java-samples.com/showtutorial.php?tutorialid=152

Post a Comment for "How To Parse Xml File On Android"