Khoá học lập trình Java Core nâng cao [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
- 5.0/5.0
- 2k Đăng ký
- Học lập trình
- Tiếng việt
Thông tin khóa học
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>Đăng nhập để làm bài kiểm tra
Chưa có kết quả nào trước đó
Chương trình
Nguyễn Văn An
Trần Thị Bích
Lê Hoàng Hải
Phạm Minh Tuấn
Đánh giá
B2. Đăng ký học
B3. Hoàn thành mua khoá học
B4. Thanh toán theo hướng dẫn
B5. Đợi chúng tôi kiểm tra thông tin và thêm bạn vào lớp học
Bước 1: Truy cập website https://gozic.vn/teacher/form
Bước 2: Điền thông tin theo yêu cầu. Để bản đăng ký được duyệt nhanh nhất, anh chị hãy điền đủ thông tin nhé.
Bước 3: Click vào "đăng ký ngay" để hoàn thành
Lưu ý:
Link video bài giảng mẫu: Video bài giảng mẫu là căn cứ để Trắc Nghiệm Việt duyệt về hình thức và chất lượng giảng dạy. Vì vậy anh chị hãy điền link này để đăng ký được duyệt nhanh nhất nhé.
Anh chị cũng nên mô tả kỹ về kinh nghiệm giảng dạy để Trắc Nghiệm Việt đánh giá nhé.
Chúc các anh chị và các bạn thành công, sớm gia nhập đội ngũ giảng viên Trắc Nghiệm Việt!
699,000 VNĐ
1,599,000 VNĐ
Tổng quan khóa học
- Bài học 88
- Thời gian Linh hoạt
- Mức độ Mới bắt đầu
- Ngôn ngữ Tiếng việt
- Chứng chỉ Không
Trần Văn Điệp
Founder tại QViet.vn