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
1. Create Project Called JavaXML.
2. Create package called com.javatutorialscorner.xml.sax under JavaXML.
3. Create Java class called Student.java under com.javatutorialscorner.xml.sax package.
Student.java
package com.javatutorialscorner.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;
}
}
4. Create Java class called SAXHandler.java under com.javatutorialscorner.xml.sax package.
SAXHandler.java
package com.javatutorialscorner.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;
}
}
}
5.Create Java class called SAXXMLReader.java under com.javatutorialscorner.xml.sax package.
SAXXMLReader.java
package com.javatutorialscorner.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();
}
}
}
6.XML file going to parse
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>
Now Run the program see the following output in console.
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
0 comments:
Post a Comment