Read XML File as List of Object using SAX Parser

In this tutorial we are going to see how to read XML file and how to convert it as List of Object using SAX parser

Student.java

package com.ehowtonow.xml.sax;

public class Student {
 private int id;
 private String firstName;
 private String lastName;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

}

SAXHandler.java

package com.ehowtonow.xml.sax;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXHandler extends DefaultHandler {

 private List<Student> studentsList = null;
 private Student student = null;

 public List<Student> getStudentsList() {
  return studentsList;
 }

 boolean firstName = false;
 boolean lastName = false;

 public void startElement(String uri, String localName, String qName,
   Attributes attribute) throws SAXException {

  if (qName.equalsIgnoreCase("STUDENT")) {
   String id = attribute.getValue("id");
   student = new Student();
   student.setId(Integer.parseInt(id));
   if (studentsList == null) {
    studentsList = new ArrayList<Student>();
   }
  } else if (qName.equalsIgnoreCase("FIRSTNAME")) {
   firstName = true;
  } else if (qName.equalsIgnoreCase("LASTNAME")) {
   lastName = true;
  }
 }

 public void endElement(String uri, String localName, String qName)
   throws SAXException {
  if (qName.equalsIgnoreCase("STUDENT")) {
  studentsList.add(student);
  }
 }

 public void characters(char ch[], int start, int length)
   throws SAXException {
  if (firstName) {
   student.setFirstName(new String(ch, start, length));
   firstName = false;
  }
  if (lastName) {
   student.setLastName(new String(ch, start, length));
   lastName = false;
  }
 }

}

SAXXMLReader.java

package com.ehowtonow.xml.sax;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

public class SAXXMLReader {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  try {
   SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
   SAXParser parser = saxParserFactory.newSAXParser();
   SAXHandler saxHandler = new SAXHandler();
   parser.parse(new File("C:\\jtc\\student.xml"), saxHandler);
   List<Student> students = saxHandler.getStudentsList();
   for (Student student : students) {

    System.out.println("Student ID : " + student.getId());
    System.out.println("Student First Name : "
      + student.getFirstName());
    System.out.println("Student Last Name : "
      + student.getLastName());

   }
  } catch (ParserConfigurationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (SAXException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

}

student.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<college>
<student id="01">
<firstname>Sathish</firstname>
<lastname>J</lastname>
</student>
<student id="02">
<firstname>Mohan</firstname>
<lastname>P</lastname>
</student>
<student id="03">
<firstname>Saravan</firstname>
<lastname>P</lastname>
</student>
<student id="04">
<firstname>Muthu</firstname>
<lastname>S</lastname>
</student>
</college>

Output

Student ID : 1
Student First Name : Sathish
Student Last Name : J
Student ID : 2
Student First Name : Mohan
Student Last Name : P
Student ID : 3
Student First Name : Saravan
Student Last Name : P
Student ID : 4
Student First Name : Muthu
Student Last Name : S


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Read XML File as List of Object using SAX Parser
  2. How to read XML file using SAX Parser
  3. XML SAX Parser Introduction
  4. How Read UTF-8 character XML file using SAX Parser

Be the first to comment

Leave a Reply