[Video] Tìm hiểu XML & Hướng dẫn phân tích tài liệu XML quản lý lớp học bằng Java
Phần nội dung:
1. Thiết kế tài liệu XML
=> mindset => tạo class object
2. Thực hành đc
=> Phân tích tài liệu XML => biến thành class object
=> Java => phân tích tài liệu XML => class object
Phân tích:
Công nghệ sử dụng:
XML => phân tích dữ liệu (đọc dữ liệu từ XML) => Parser (Library - Java, PHP, NodeJS, ...)
Parser
=> DOM Parser
=> Ưu điểm: Phân tích được tất cả các loại tài liệu XML => ko cần biết trc cấu trúc của XML
=> Nhược:
- Trậm, tốn tài nguyên
=> JAX Parser
=> Ưu điểm: nhẹ chương trình, nhanh
=> Nhược: Mỗi một tài liệu XML => xây dựng 1 JAX tương ứng cho tài liệu đó. Biết trc cấu trúc của XML cần phân tích
=> Sử dụng cái này.
Thuật ngữ sử dụng:
XPath
class_list/class/classname => dữ liệu trả về là gì (DOM Parser)
=> phần tử đầu
Xây dựng dự án Java:
- Đầu vào là file student.xml
- Phân tích cấu trúc xml => mapping class object tương ứng
- Xây dựng Jax parser => chuyển student.xml => object trong java
- Xay dung Parser cho student.xml
- Đầu ra của chúng ta => object trong java
Source Code
/*
* 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 lession1;
import java.io.FileInputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
/**
*
* @author Diep.Tran
*/
public class Main {
public static void main(String[] args) throws Exception {
//doc noi dung tu file xml.
FileInputStream fis = new FileInputStream("aptech_class.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
AptechClassHandler aptechClassHandler = new AptechClassHandler();
saxParser.parse(fis, aptechClassHandler);
aptechClassHandler.display();
}
}
/*
* 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 lession1;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Diep.Tran
*/
public class AptechClass {
String classname, address;
Teacher teacher;
List<Student> studentList = new ArrayList<>();
public AptechClass() {
}
public String getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public List<Student> getStudentList() {
return studentList;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}
public void display() {
System.out.format("\nClass Name: %s, Address: %s", classname, address);
System.out.println("Teacher's Detail information: ");
System.out.println(teacher);
System.out.println("Student List: ");
for (Student student : studentList) {
System.out.println(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 lession1;
/**
*
* @author Diep.Tran
*/
public class Teacher {
String fullname, address;
int age;
public Teacher() {
}
public Teacher(String fullname, String address, int age) {
this.fullname = fullname;
this.address = address;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Teacher{" + "fullname=" + fullname + ", address=" + address + ", age=" + age + '}';
}
}
/*
* 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 lession1;
/**
*
* @author Diep.Tran
*/
public class Student {
String fullname, address;
int age;
public Student(String fullname, String address, int age) {
this.fullname = fullname;
this.address = address;
this.age = age;
}
public Student() {
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" + "fullname=" + fullname + ", address=" + address + ", age=" + age + '}';
}
}
/*
* 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 lession1;
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 Diep.Tran
*/
public class AptechClassHandler extends DefaultHandler{
List<AptechClass> classList = new ArrayList<>();
AptechClass currentClass = null;
Student currentStudent = null;
boolean isClassName = false;
boolean isAddress = false;
boolean isTeacher = false;
boolean isFullname = false;
boolean isAge = false;
boolean isStudent = false;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if(qName.equalsIgnoreCase("class")) {
currentClass = new AptechClass();
} else if(qName.equalsIgnoreCase("classname")) {
isClassName = true;
} else if(qName.equalsIgnoreCase("address")) {
isAddress = true;
} else if(qName.equalsIgnoreCase("teacher")) {
isTeacher = true;
Teacher teacher = new Teacher();
currentClass.setTeacher(teacher);
} else if(qName.equalsIgnoreCase("fullname")) {
isFullname = true;
} else if(qName.equalsIgnoreCase("age")) {
isAge = true;
} else if(qName.equalsIgnoreCase("student")) {
isStudent = true;
currentStudent = new Student();
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if(qName.equalsIgnoreCase("class")) {
classList.add(currentClass);
currentClass = null;
} else if(qName.equalsIgnoreCase("classname")) {
isClassName = false;
} else if(qName.equalsIgnoreCase("address")) {
isAddress = false;
} else if(qName.equalsIgnoreCase("teacher")) {
isTeacher = false;
} else if(qName.equalsIgnoreCase("fullname")) {
isFullname = false;
} else if(qName.equalsIgnoreCase("age")) {
isAge = false;
} else if(qName.equalsIgnoreCase("student")) {
isStudent = false;
currentClass.getStudentList().add(currentStudent);
currentStudent = null;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String value = new String(ch, start, length);
if(isClassName) {
currentClass.setClassname(value);
} else if(isAddress) {
if(isTeacher) {
currentClass.getTeacher().setAddress(value);
} else if(isStudent) {
currentStudent.setAddress(value);
} else {
currentClass.setAddress(value);
}
} else if(isFullname) {
if(isTeacher) {
currentClass.getTeacher().setFullname(value);
} else if(isStudent) {
currentStudent.setFullname(value);
}
} else if(isAge) {
if(isTeacher) {
currentClass.getTeacher().setAge(Integer.parseInt(value));
} else if(isStudent) {
currentStudent.setAge(Integer.parseInt(value));
}
}
}
public void display() {
for (AptechClass aptechClass : classList) {
aptechClass.display();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<class_list title="sdsd" abc="sasd">
<class>
<classname>T1907A</classname>
<address>8 Ton That Thuyet, Cau Giay, HN</address>
<teacher>
<fullname>Tran Van A</fullname>
<age>32</age>
<address>Nam Dinh</address>
</teacher>
<student_list>
<student>
<fullname>Nguyen Van A</fullname>
<age>18</age>
<address>Ha Noi</address>
</student>
<student>
<fullname>Nguyen Van B</fullname>
<age>18</age>
<address>Ha Noi</address>
</student>
<student>
<fullname>Nguyen Van C</fullname>
<age>18</age>
<address>Ha Noi</address>
</student>
</student_list>
</class>
<class>
<classname>T1907E</classname>
<address>8 Ton That Thuyet, Cau Giay, HN</address>
<teacher>
<fullname>Tran Van B</fullname>
<age>32</age>
<address>Nam Dinh</address>
</teacher>
<student_list>
<student>
<fullname>Nguyen Van A</fullname>
<age>18</age>
<address>Ha Noi</address>
</student>
<student>
<fullname>Nguyen Van B</fullname>
<age>18</age>
<address>Ha Noi</address>
</student>
<student>
<fullname>Nguyen Van C</fullname>
<age>18</age>
<address>Ha Noi</address>
</student>
</student_list>
</class>
</class_list>
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Phan Bạch Tùng Dương [T1907A]](https://www.gravatar.com/avatar/e74e3ec62fe3a191929e12eecbe01edf.jpg?s=80&d=mm&r=g)
Phan Bạch Tùng Dương
2020-04-15 10:07:29
/*
* 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 ClassInfo;
import java.io.FileInputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
/**
*
* @author Admin
*/
public class Main {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("aptech_class.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
AptechClassHandler aptechClassHandler = new AptechClassHandler();
saxParser.parse(fis, aptechClassHandler);
aptechClassHandler.display();
}
}
/*
* 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 ClassInfo;
/**
*
* @author Admin
*/
public class Student {
String fullname, address;
int age;
public Student(String fullname, String address, int age) {
this.fullname = fullname;
this.address = address;
this.age = age;
}
public Student() {
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" + "fullname=" + fullname + ", address=" + address + ", age=" + age + '}';
}
}
/*
* 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 ClassInfo;
/**
*
* @author Admin
*/
public class Teacher {
String fullname, address;
int age;
public Teacher() {
}
public Teacher(String fullname, String address, int age) {
this.fullname = fullname;
this.address = address;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Teacher{" + "fullname=" + fullname + ", address=" + address + ", age=" + age + '}';
}
}
/*
* 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 ClassInfo;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Admin
*/
public class AptechClass {
String classname, address;
Teacher teacher;
List<Student> studentList = new ArrayList<>();
public AptechClass() {
}
public String getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public List<Student> getStudentList() {
return studentList;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}
public void display() {
System.out.format("\nClass Name: %s, Address: %s", classname, address);
System.out.println("Teacher's Detail information: ");
System.out.println(teacher);
System.out.println("Student List: ");
for (Student student : studentList) {
System.out.println(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 ClassInfo;
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 Diep.Tran
*/
public class AptechClassHandler extends DefaultHandler {
List<AptechClass> classList = new ArrayList<>();
AptechClass currentClass = null;
Student currentStudent = null;
boolean isClassName = false;
boolean isAddress = false;
boolean isTeacher = false;
boolean isFullname = false;
boolean isAge = false;
boolean isStudent = false;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("class")) {
currentClass = new AptechClass();
} else if (qName.equalsIgnoreCase("classname")) {
isClassName = true;
} else if (qName.equalsIgnoreCase("address")) {
isAddress = true;
} else if (qName.equalsIgnoreCase("teacher")) {
isTeacher = true;
Teacher teacher = new Teacher();
currentClass.setTeacher(teacher);
} else if (qName.equalsIgnoreCase("fullname")) {
isFullname = true;
} else if (qName.equalsIgnoreCase("age")) {
isAge = true;
} else if (qName.equalsIgnoreCase("student")) {
isStudent = true;
currentStudent = new Student();
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase("class")) {
classList.add(currentClass);
currentClass = null;
} else if (qName.equalsIgnoreCase("classname")) {
isClassName = false;
} else if (qName.equalsIgnoreCase("address")) {
isAddress = false;
} else if (qName.equalsIgnoreCase("teacher")) {
isTeacher = false;
} else if (qName.equalsIgnoreCase("fullname")) {
isFullname = false;
} else if (qName.equalsIgnoreCase("age")) {
isAge = false;
} else if (qName.equalsIgnoreCase("student")) {
isStudent = false;
currentClass.getStudentList().add(currentStudent);
currentStudent = null;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String value = new String(ch, start, length);
if (isClassName) {
currentClass.setClassname(value);
} else if (isAddress) {
if (isTeacher) {
currentClass.getTeacher().setAddress(value);
} else if (isStudent) {
currentStudent.setAddress(value);
} else {
currentClass.setAddress(value);
}
} else if (isFullname) {
if (isTeacher) {
currentClass.getTeacher().setFullname(value);
} else if (isStudent) {
currentStudent.setFullname(value);
}
} else if (isAge) {
if (isTeacher) {
currentClass.getTeacher().setAge(Integer.parseInt(value));
} else if (isStudent) {
currentStudent.setAge(Integer.parseInt(value));
}
}
}
public void display() {
classList.forEach((aptechClass) -> {
aptechClass.display();
});
}
}