Tạo bởi Trắc Nghiệm Việt|
Share Code - Hướng dẫn sử dụng Sax Parser phân tích tài liệu xml - danh sách sinh viên
Java Web + WebService
Share Code - Hướng dẫn sử dụng Sax Parser phân tích tài liệu xml - danh sách sinh viên
Share Code - Hướng dẫn sử dụng Sax Parser phân tích tài liệu xml - danh sách sinh viên
Tài liệu xml >> Danh sách sinh viên
<?xml version="1.0" encoding="UTF-8"?>
<root>
<studentList>
<student>
<fullname>Tran Van A</fullname>
<age>22</age>
<address>Ha Noi</address>
<email>tranvandiep.it@gmail.com</email>
<roll_no>R001</roll_no>
</student>
<student>
<fullname>Tran Van B</fullname>
<age>22</age>
<address>Ha Noi</address>
<email>tranvandiep.it@gmail.com</email>
<roll_no>R001</roll_no>
</student>
</studentList>
</root>
Thực hiện tạo class Student với các thuộc tính như mô tả trong tài liệu XML
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package aptech;
/**
*
* @author DiepTV
*/
public class Student {
String fullname, address, email, rollNo;
int age;
public Student() {
}
public Student(String fullname, String address, String email, String rollNo, int age) {
this.fullname = fullname;
this.address = address;
this.email = email;
this.rollNo = rollNo;
this.age = age;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getRollNo() {
return rollNo;
}
public void setRollNo(String rollNo) {
this.rollNo = rollNo;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" + "fullname=" + fullname + ", address=" + address + ", email=" + email + ", rollNo=" + rollNo + ", age=" + age + '}';
}
public void display() {
System.out.println(this);
}
}
Viết một bộ phân tích SAX Parser => Thực hiện nhiệm vụ chuyển đổi dữ liệu từ tài liệu xml thành đối tượng Student
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package aptech;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
*
* @author DiepTV
*/
public class StudentHandler extends DefaultHandler {
List<Student> studentList;
boolean isFullname = false, isAge = false, isEmail = false, isAddress = false, isRollNo = false;
Student currentStudent;
public StudentHandler() {
studentList = new ArrayList<>();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("Student")) {
currentStudent = new Student();
} else if (qName.equalsIgnoreCase("fullname")) {
isFullname = true;
} else if (qName.equalsIgnoreCase("Age")) {
isAge = true;
} else if (qName.equalsIgnoreCase("Email")) {
isEmail = true;
} else if (qName.equalsIgnoreCase("Address")) {
isAddress = true;
} else if (qName.equalsIgnoreCase("Roll_No")) {
isRollNo = true;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase("Student")) {
studentList.add(currentStudent);
currentStudent = null;
} else if (qName.equalsIgnoreCase("fullname")) {
isFullname = false;
} else if (qName.equalsIgnoreCase("Age")) {
isAge = false;
} else if (qName.equalsIgnoreCase("Email")) {
isEmail = false;
} else if (qName.equalsIgnoreCase("Address")) {
isAddress = false;
} else if (qName.equalsIgnoreCase("Roll_No")) {
isRollNo = false;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (currentStudent != null) {
if (isFullname) {
currentStudent.setFullname(new String(ch, start, length));
} else if (isAge) {
String ageStr = new String(ch, start, length);
currentStudent.setAge(Integer.parseInt(ageStr));
} else if (isEmail) {
currentStudent.setEmail(new String(ch, start, length));
} else if (isAddress) {
currentStudent.setAddress(new String(ch, start, length));
} else if (isRollNo) {
currentStudent.setRollNo(new String(ch, start, length));
}
}
}
public List<Student> getStudentList() {
return studentList;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}
}
Viết chương trình test. Nối các thành phần trong dự án.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package aptech;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
/**
*
* @author DiepTV
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
// TODO code application logic here
File file = new File("student.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
StudentHandler studentHandler = new StudentHandler();
parser.parse(file, studentHandler);
//lay dc data
List<Student> studentList = studentHandler.getStudentList();
for (Student student : studentList) {
student.display();
}
} catch (ParserConfigurationException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (SAXException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}