[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)
![Hoang Ngo [T1907A]](https://www.gravatar.com/avatar/9f7d962f002d4b5c555b1ee25b3622ff.jpg?s=80&d=mm&r=g)
Hoang Ngo
2020-04-24 07:02:23
/*
* 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 bai1;
import java.io.FileInputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
/**
*
* @author PC
*/
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 bai1;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author PC
*/
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 bai1;
/**
*
* @author PC
*/
public class Teacher {
String fullname,
String 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 bai1;
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 PC
*/
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();
}
}
}
![nguyễn văn huy [T1907A]](https://www.gravatar.com/avatar/b107d14d7d43c142b68c12c377262371.jpg?s=80&d=mm&r=g)
nguyễn văn huy
2020-04-18 15:36:25
/*
* 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 XML;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
/**
*
* @author ASUS
*/
public class Main {
public static void main(String[] args) throws Exception {
FileInputStream fis=new FileInputStream("C:\\Users\\ASUS\\Documents\\NetBeansProjects\\yyy\\src\\XML\\student.xml");
SAXParserFactory fac=SAXParserFactory.newInstance();
SAXParser sax=fac.newSAXParser();
parser pa=new parser();
sax.parse(fis, pa);
pa.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 XML;
/**
*
* @author ASUS
*/
public class Student {
String name,address;
int age;
public Student() {
}
public Student(String name, String address, int age) {
this.name = name;
this.address = address;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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{" + "name=" + name + ", 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 XML;
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 ASUS
*/
public class parser extends DefaultHandler {
List<teacher> tealist = new ArrayList<>();
teacher tea = null;
boolean isName = false;
boolean isTeachername = false;
boolean isAge = false;
boolean isAddress = false;
boolean isStudent=false;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("class")) {
tea=new teacher();
}else if(qName.equalsIgnoreCase("name")){
isName=true;
}else if(qName.equalsIgnoreCase("teachername")){
isTeachername=true;
}else if(qName.equalsIgnoreCase("address")){
isAddress=true;
}else if(qName.equalsIgnoreCase("age")){
isAge=true;
}else if(qName.equalsIgnoreCase("student")){
isStudent=true;
Student thao=new Student();
tea.setThao(thao);
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if(qName.equalsIgnoreCase("class")){
tealist.add(tea);
tea=null;
}else if(qName.equalsIgnoreCase("name")){
isName=false;
}else if(qName.equalsIgnoreCase("teachername")){
isTeachername=false;
}else if(qName.equalsIgnoreCase("address")){
isAddress=false;
}else if(qName.equalsIgnoreCase("age")){
isAge=false;
}else if(qName.equalsIgnoreCase("student")){
isStudent=false;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String valuu=new String(ch,start,length);
if(isName){
if(isStudent){
tea.thao.setName(valuu);
}else{
tea.setName(valuu);
}
}else if(isTeachername){
tea.setTeachername(valuu);
}else if(isAddress){
if(isStudent){
tea.thao.setAddress(valuu);
}else{
tea.setAddress(valuu);
}
}else if(isAge){
if(isStudent){
tea.thao.setAge(Integer.parseInt(valuu));
}else{
tea.setAge(Integer.parseInt(valuu));
}
}
}
public void display(){
for (teacher teaaa : tealist) {
teaaa.display();
}
}
}
>>>>>>
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<class_list title="sdsd" abc="sasd">
<class>
<name>A</name>
<teachname>nguyen van a</teachname>
<address>FPT</address>
<student_list>
<student>
<name> nguyen van huy</name>
<address>Duong Quang Ham</address>
</student>
<student>
<name> nguyen van B</name>
<address>Duong Quang Ham</address>
</student>
</student_list>
</class>
<class>
<name>AA</name>
<teachername>Nguyen Văn B</teachername>
<address>FPT Aptech</address>
<student_list>
<student>
<name>nguyen van A</name>
<address>Ha Noi</address>
</student>
<student>
<name>nguyen van B</name>
<address>Thai Binh</address>
</student>
</student_list>
</class>
</class_list>
>>>>>
/*
* 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 XML;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author ASUS
*/
public class teacher {
String name, teachername, address;
int age;
Student thao;
public teacher() {
}
public teacher(String name, String teachername, String address, int age, Student thao) {
this.name = name;
this.teachername = teachername;
this.address = address;
this.age = age;
this.thao = thao;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTeachername() {
return teachername;
}
public void setTeachername(String teachername) {
this.teachername = teachername;
}
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;
}
public Student getThao() {
return thao;
}
public void setThao(Student thao) {
this.thao = thao;
}
public void display(){
System.out.format("\nName:%s,Teachername:%s,Address:%s,Age:%s",name,teachername,address,age);
System.out.println(thao);
}
}
![Trần Anh Quân [T1907A]](https://www.gravatar.com/avatar/7e3a8fe30cedd711f426fc59a9d48efc.jpg?s=80&d=mm&r=g)
Trần Anh Quân
2020-04-18 14:24:03
/*
* 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 ls1;
import java.io.FileInputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
/**
*
* @author Anh Quan
*/
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 ls1;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Anh Quan
*/
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 ls1;
/**
*
* @author Anh Quan
*/
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 ls1;
/**
*
* @author Anh Quan
*/
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 ls1;
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 Anh Quan
*/
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"?>
<!--
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.
-->
<class_list title="sdsd" abc="sasd">
<class>
<classname>T1907A</classname>
<address>8 Ton That Thuyet, Cau Giay, HN</address>
<teacher>
<fullname>Tran Van Diep</fullname>
<age>30</age>
<address>Ha Noi</address>
</teacher>
<student_list>
<student>
<fullname>Tran Anh Quan</fullname>
<age>19</age>
<address>Ha Noi</address>
</student>
<student>
<fullname>Tran Anh Tu</fullname>
<age>19</age>
<address>Ha Noi</address>
</student>
<student>
<fullname>Tran Anh Hung</fullname>
<age>19</age>
<address>Ha Noi</address>
</student>
</student_list>
</class>
<class>
<classname>T1908K</classname>
<address>8 Ton That Thuyet, Cau Giay, HN</address>
<teacher>
<fullname>Nguyen Tuan</fullname>
<age>30</age>
<address>Ha Noi</address>
</teacher>
<student_list>
<student>
<fullname>Tran Anh A</fullname>
<age>19</age>
<address>Ha Noi</address>
</student>
<student>
<fullname>Tran Anh B</fullname>
<age>19</age>
<address>Ha Noi</address>
</student>
<student>
<fullname>Tran Anh C</fullname>
<age>19</age>
<address>Ha Noi</address>
</student>
</student_list>
</class>
</class_list>
![NguyenHuuThanh [T1907A]](https://www.gravatar.com/avatar/035e4f4fed661b8e1c3e066e43cd5e41.jpg?s=80&d=mm&r=g)
NguyenHuuThanh
2020-04-17 05:56:26
/*
* 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 aptechclass;
import java.io.FileInputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
/**
*
* @author abc
*/
public class Main {
public static void main(String[] args) throws Exception
{
FileInputStream fis = new FileInputStream("student.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 aptechclass;
/**
*
* @author abc
*/
public class Student {
String fullname ,address ;
int age ;
public Student()
{
}
public Student(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 "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 aptechclass;
/**
*
* @author abc
*/
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 aptechclass;
import java.util.ArrayList;
import java.util.List;
import jdk.internal.org.xml.sax.SAXException;
import jdk.internal.org.xml.sax.helpers.DefaultHandler;
/**
*
* @author abc
*/
public class AptechClasshandler extends DefaultHandler {
List<AptechClass> classList = new ArrayList<>();
AptechClass currentClass = null;
Student currentStudent = null;
// neu co list thi phai dung
boolean isClassName = false ;
boolean isAddress = false;
boolean isTeacher = false;
boolean isFullname = false;
boolean isAge = false;
boolean isStudent = false;
@Override
public void startElement(String url , String localName , String qName) 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();
}
}
}
/*
* 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 aptechclass;
import java.io.FileInputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
/**
*
* @author abc
*/
public class Main {
public static void main(String[] args) throws Exception
{
FileInputStream fis = new FileInputStream("student.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
AptechClasshandler aptechClasshandler = new AptechClasshandler();
saxParser.parse(fis, aptechClasshandler);
aptechClasshandler.display();
}
}
![Đường Thanh Bình [T1907A]](https://www.gravatar.com/avatar/c2ef7c316acb82467912bf5677b52a8b.jpg?s=80&d=mm&r=g)
Đường Thanh Bình
2020-04-16 14:16:08
<?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 Diep</fullname>
<age>32</age>
<address>Nam Dinh</address>
</teacher>
<student_list>
<student>
<fullname>Duong Thanh Binh</fullname>
<age>18</age>
<address>Ha Noi</address>
</student>
<student>
<fullname>Phi Van Long</fullname>
<age>18</age>
<address>Ha Noi</address>
</student>
<student>
<fullname>Nguyen Minh Duy</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>Nguy Tuan</fullname>
<age>32</age>
<address>Ha Noi</address>
</teacher>
<student_list>
<student>
<fullname>Nguyen Van Phuong</fullname>
<age>18</age>
<address>Ha Noi</address>
</student>
<student>
<fullname>Nguyen Minh Nghia</fullname>
<age>18</age>
<address>Ha Noi</address>
</student>
<student>
<fullname>Duong Thanh B</fullname>
<age>18</age>
<address>Ha Noi</address>
</student>
</student_list>
</class>
</class_list>
/*
* 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 Administrator
*/
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 Lession1;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Administrator
*/
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 Administrator
*/
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 Administrator
*/
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 Administrator
*/
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();
}
}
}
![Trần Mạnh Dũng [T1907A]](https://www.gravatar.com/avatar/ee4661d1f9133c241d6c8287c7ea8ceb.jpg?s=80&d=mm&r=g)
Trần Mạnh Dũng
2020-04-16 10:24:40
<?xml version="1.0" encoding="UTF-8"?>
<classaptech_list>
<class>
<name>A1</name>
<teachername>Trần Văn A</teachername>
<studentnumber>20</studentnumber>
<address>FPT Aptech</address>
<student_list>
<student>
<name>Trần Mạnh Dũng</name>
<age>18</age>
<phone>0123456789</phone>
</student>
<student>
<name>Trần Yếu Dũng</name>
<age>18</age>
<phone>0987654321</phone>
</student>
</student_list>
</class>
<class>
<name>A2</name>
<teachername>Trần Văn B</teachername>
<studentnumber>20</studentnumber>
<address>FPT Aptech</address>
<student_list>
<student>
<name>Trần Mạnh A</name>
<age>18</age>
<phone>09358345</phone>
</student>
<student>
<name>Trần Mạnh B</name>
<age>18</age>
<phone>098734634</phone>
</student>
</student_list>
</class>
</classaptech_list>
/*
* 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.FileInputStream;
import java.io.FileNotFoundException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
/**
*
* @author admin
*/
public class Main{
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
// TODO code application logic here
FileInputStream fis = new FileInputStream("text.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 aptech;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author admin
*/
public class AptechClass {
String name, teachername, address, studentnumber;
Student student;
List<Student> studentList = new ArrayList<>();
public AptechClass() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTeachername() {
return teachername;
}
public void setTeachername(String teachername) {
this.teachername = teachername;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getStudentnumber() {
return studentnumber;
}
public void setStudentnumber(String studentnumber) {
this.studentnumber = studentnumber;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public List<Student> getStudentList() {
return studentList;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}
public void display(){
System.out.format("Class Name: %s, TeacherName: %s, Address: %s, StudentNumber: %s", name, teachername, address, studentnumber);
System.out.println("\nStudent 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 aptech;
/**
*
* @author admin
*/
public class Student {
String name, phone;
int age;
public Student() {
}
public Student(String name, String phone, int age) {
this.name = name;
this.phone = phone;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" + "name=" + name + ", phone=" + phone + ", 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 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 admin
*/
public class AptechClassHandler extends DefaultHandler{
List<AptechClass> classList = new ArrayList<>();
AptechClass currentClass = null;
Student currentStudent = null;
boolean isName = false;
boolean isTeachername = false;
boolean isStudentnumber = false;
boolean isAddress = false;
boolean isStudent = false;
boolean isAge = false;
boolean isPhone = 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("name")){
isName = true;
}else if(qName.equalsIgnoreCase("teachername")){
isTeachername = true;
}else if(qName.equalsIgnoreCase("studentnumber")){
isStudentnumber = true;
}else if(qName.equalsIgnoreCase("address")){
isAddress = true;
}else if(qName.equalsIgnoreCase("student")){
isStudent = true;
currentStudent = new Student();
}else if(qName.equalsIgnoreCase("age")){
isAge = true;
}else if(qName.equalsIgnoreCase("phone")){
isPhone = true;
}
}
@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("name")){
isName = false;
}else if(qName.equalsIgnoreCase("teachername")){
isTeachername = false;
}else if(qName.equalsIgnoreCase("studentnumber")){
isStudentnumber = false;
}else if(qName.equalsIgnoreCase("address")){
isAddress = false;
}else if(qName.equalsIgnoreCase("student")){
isStudent = false;
currentClass.getStudentList().add(currentStudent);
currentStudent = null;
}else if(qName.equalsIgnoreCase("age")){
isAge = false;
}else if(qName.equalsIgnoreCase("phone")){
isPhone = false;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String value = new String(ch, start, length);
if(isName){
if (isStudent){
currentStudent.setName(value);
} else{
currentClass.setName(value);
}
}else if(isTeachername){
currentClass.setTeachername(value);
}else if(isAddress){
currentClass.setAddress(value);
}
else if(isStudentnumber){
currentClass.setStudentnumber(value);
}else if(isAge){
currentStudent.setAge(Integer.parseInt(value));
}else if(isPhone){
currentStudent.setPhone(value);
}
}
public void display(){
for (AptechClass aptechClass : classList) {
aptechClass.display();
}
}
}
![Minh Nghia [T1907A]](https://www.gravatar.com/avatar/ecca255d725eed36a872105205af1b8e.jpg?s=80&d=mm&r=g)
Minh Nghia
2020-04-15 16:32:27
/*
* 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 studentxml;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Administrator
*/
public class AptechClass {
String classname, address;
Teacher teacher;
List<Student> studentList = new ArrayList<>();
public AptechClass() {
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
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 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 studentxml;
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 Administrator
*/
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 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();
});
}
}
/*
* 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 studentxml;
/**
*
* @author Administrator
*/
public class Student {
String fullname, address;
int age;
public Student() {
}
public Student(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 "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 studentxml;
/**
*
* @author Administrator
*/
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 studentxml;
import java.io.FileInputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
/**
*
* @author Administrator
*/
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();
}
}
![hoangduyminh [T1907A]](https://www.gravatar.com/avatar/33675cc9fc3762fd323389a179aa047f.jpg?s=80&d=mm&r=g)
hoangduyminh
2020-04-15 14:02:13
/*
* 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 t1907ac;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
/**
*
* @author Minh
*/
public class T1907Ac {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
// TODO code application logic here
FileInputStream fis = new FileInputStream("class.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
AptechClassParser apptechClassParser = new AptechClassParser();
saxParser.parse(fis, apptechClassParser);
apptechClassParser.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 t1907ac;
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 Minh
*/
public class AptechClassParser 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();
}
}
}
/*
* 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 t1907ac;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Minh
*/
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 infornation: ");
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 t1907ac;
/**
*
* @author Minh
*/
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 t1907ac;
/**
*
* @author Minh
*/
public class Student {
String fullname, address;
int age;
public Student() {
}
public Student(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 "Student{" + "fullname=" + fullname + ", address=" + address + ", age=" + age + '}';
}
}
![Phí Văn Long [T1907A]](https://www.gravatar.com/avatar/5db166b7b74443c5c221e4c0068d6da9.jpg?s=80&d=mm&r=g)
Phí Văn Long
2020-04-15 12:55:36
/*
* 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 lesson1;
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 {
//doc noi dung tu file Aptech_class.xml.
FileInputStream fis = new FileInputStream("Aptech_class.xml");
// SAXParser thông báo cho các trình khách cấu trúc của tài liệu XML bằng cách gọi các hàm callbacks => hoạt động nhanh , ít tốn bộ nhớ
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
AptechClassHandler aptechClassHandler = new AptechClassHandler();
saxParser.parse(fis, aptechClassHandler);
//hiển thị
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 lesson1;
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("\n Class Name: %s,Address: %s",classname,address);
System.out.println("\n 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 lesson1;
/**
*
* @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 lesson1;
/**
*
* @author Admin
*/
public class Student {
String fullname,address;
int age;
public Student() {
}
public Student(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 "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 lesson1;
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 Admin
*/
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();
}
}
}
![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:08:44
<?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>Phan Bach Tung Duong</fullname>
<age>18</age>
<address>Ha Noi</address>
</teacher>
<student_list>
<student>
<fullname>Phan Bach Tung A</fullname>
<age>19</age>
<address>Ha Noi</address>
</student>
<student>
<fullname>Phan Bach Tung B</fullname>
<age>21</age>
<address>Ha Noi</address>
</student>
<student>
<fullname>Phan Bach Tung C</fullname>
<age>22</age>
<address>Ha Noi</address>
</student>
</student_list>
</class>
<class>
<classname>T1908K</classname>
<address>8 Ton That Thuyet, Cau Giay, HN</address>
<teacher>
<fullname>Phan Bach Tung G</fullname>
<age>18</age>
<address>Ha Noi</address>
</teacher>
<student_list>
<student>
<fullname>Phan Bach Tung H</fullname>
<age>19</age>
<address>Ha Noi</address>
</student>
<student>
<fullname>Phan Bach Tung S</fullname>
<age>20</age>
<address>Ha Noi</address>
</student>
<student>
<fullname>Phan Bach Tung N</fullname>
<age>21</age>
<address>Ha Noi</address>
</student>
</student_list>
</class>
</class_list>