By GokiSoft.com| 13:21 30/03/2020|
Java Web + WebService

Share Code - Hướng dẫn sử dụng Sax Parser phân tích tài liệu xml - danh sách sinh viên

Share Code - Hướng dẫn sử dụng Sax Parser phân tích tài liệu xml - danh sách sinh viên


Tài liệu xml >> Danh sách sinh viên


<?xml version="1.0" encoding="UTF-8"?>
<root>
	<studentList>
		<student>
			<fullname>Tran Van A</fullname>
			<age>22</age>
			<address>Ha Noi</address>
			<email>tranvandiep.it@gmail.com</email>
			<roll_no>R001</roll_no>
		</student>
		<student>
			<fullname>Tran Van B</fullname>
			<age>22</age>
			<address>Ha Noi</address>
			<email>tranvandiep.it@gmail.com</email>
			<roll_no>R001</roll_no>
		</student>
	</studentList>
</root>

Thực hiện tạo class Student với các thuộc tính như mô tả trong tài liệu XML

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech;

/**
 *
 * @author DiepTV
 */
public class Student {
    String fullname, address, email, rollNo;
    int age;

    public Student() {
    }

    public Student(String fullname, String address, String email, String rollNo, int age) {
        this.fullname = fullname;
        this.address = address;
        this.email = email;
        this.rollNo = rollNo;
        this.age = age;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getRollNo() {
        return rollNo;
    }

    public void setRollNo(String rollNo) {
        this.rollNo = rollNo;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" + "fullname=" + fullname + ", address=" + address + ", email=" + email + ", rollNo=" + rollNo + ", age=" + age + '}';
    }
    
    public void display() {
        System.out.println(this);
    }
}

Viết một bộ phân tích SAX Parser => Thực hiện nhiệm vụ chuyển đổi dữ liệu từ tài liệu xml thành đối tượng Student

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech;

import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author DiepTV
 */
public class StudentHandler extends DefaultHandler {

    List<Student> studentList;
    boolean isFullname = false, isAge = false, isEmail = false, isAddress = false, isRollNo = false;
    Student currentStudent;

    public StudentHandler() {
        studentList = new ArrayList<>();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("Student")) {
            currentStudent = new Student();
        } else if (qName.equalsIgnoreCase("fullname")) {
            isFullname = true;
        } else if (qName.equalsIgnoreCase("Age")) {
            isAge = true;
        } else if (qName.equalsIgnoreCase("Email")) {
            isEmail = true;
        } else if (qName.equalsIgnoreCase("Address")) {
            isAddress = true;
        } else if (qName.equalsIgnoreCase("Roll_No")) {
            isRollNo = true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("Student")) {
            studentList.add(currentStudent);
            currentStudent = null;
        } else if (qName.equalsIgnoreCase("fullname")) {
            isFullname = false;
        } else if (qName.equalsIgnoreCase("Age")) {
            isAge = false;
        } else if (qName.equalsIgnoreCase("Email")) {
            isEmail = false;
        } else if (qName.equalsIgnoreCase("Address")) {
            isAddress = false;
        } else if (qName.equalsIgnoreCase("Roll_No")) {
            isRollNo = false;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (currentStudent != null) {
            if (isFullname) {
                currentStudent.setFullname(new String(ch, start, length));
            } else if (isAge) {
                String ageStr = new String(ch, start, length);
                currentStudent.setAge(Integer.parseInt(ageStr));
            } else if (isEmail) {
                currentStudent.setEmail(new String(ch, start, length));
            } else if (isAddress) {
                currentStudent.setAddress(new String(ch, start, length));
            } else if (isRollNo) {
                currentStudent.setRollNo(new String(ch, start, length));
            }
        }
    }

    public List<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }
}

Viết chương trình test. Nối các thành phần trong dự án.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;

/**
 *
 * @author DiepTV
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            // TODO code application logic here
            File file = new File("student.xml");
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            
            StudentHandler studentHandler = new StudentHandler();
            
            parser.parse(file, studentHandler);
            
            //lay dc data
            List<Student> studentList = studentHandler.getStudentList();
            
            for (Student student : studentList) {
                student.display();
            }
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}


Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Phạm Ngọc Minh [T1907A]
Phạm Ngọc Minh

2020-04-16 09:43:59



<?xml version="1.0" encoding="UTF-8"?>
<student_list>
    <student>
        <fullname>Pham Ngoc Minh</fullname>
        <age>19</age>
        <address>Ha Tinh</address>
        <email>minhpn1907007@gmail.com</email>
        <rollNo>R001</rollNo>
    </student>
    <student>
        <fullname>Pham Ngoc A</fullname>
        <age>20</age>
        <address>Ha Noi</address>
        <email>realmadridcr@gmail.com</email>
        <rollNo>R001</rollNo>
    </student>
</student_list>



package T1907AXML;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
public class Bt12345 {
	public static void main(String[] args) {
        try {
            
            File file = new File("NewFile1.xml");
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            
            StudentHandler studentHandler = new StudentHandler();
            
            parser.parse(file, studentHandler);
                     
            List<Student> studentList = studentHandler.getStudentList();
            
            for (Student student : studentList) {
                student.display();
            }
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(Bt12345.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(Bt12345.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Bt12345.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}




package T1907AXML;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class StudentHandler extends DefaultHandler {
	List<Student> studentList;
    boolean isFullname = false, isAge = false, isEmail = false, isAddress = false, isRollNo = false;
    Student currentStudent;

    public StudentHandler() {
        studentList = new ArrayList<>();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("Student")) {
            currentStudent = new Student();
        } else if (qName.equalsIgnoreCase("fullname")) {
            isFullname = true;
        } else if (qName.equalsIgnoreCase("Age")) {
            isAge = true;
        } else if (qName.equalsIgnoreCase("Email")) {
            isEmail = true;
        } else if (qName.equalsIgnoreCase("Address")) {
            isAddress = true;
        } else if (qName.equalsIgnoreCase("Roll_No")) {
            isRollNo = true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("Student")) {
            studentList.add(currentStudent);
            currentStudent = null;
        } else if (qName.equalsIgnoreCase("fullname")) {
            isFullname = false;
        } else if (qName.equalsIgnoreCase("Age")) {
            isAge = false;
        } else if (qName.equalsIgnoreCase("Email")) {
            isEmail = false;
        } else if (qName.equalsIgnoreCase("Address")) {
            isAddress = false;
        } else if (qName.equalsIgnoreCase("Roll_No")) {
            isRollNo = false;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (currentStudent != null) {
            if (isFullname) {
                currentStudent.setFullname(new String(ch, start, length));
            } else if (isAge) {
                String ageStr = new String(ch, start, length);
                currentStudent.setAge(Integer.parseInt(ageStr));
            } else if (isEmail) {
                currentStudent.setEmail(new String(ch, start, length));
            } else if (isAddress) {
                currentStudent.setAddress(new String(ch, start, length));
            } else if (isRollNo) {
                currentStudent.setRollNo(new String(ch, start, length));
            }
        }
    }

    public List<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }
}




package T1907AXML;

public class Student {
	String fullname, address, email, rollNo;
    int age;

    public Student() {
    }

    public Student(String fullname, String address, String email, String rollNo, int age) {
        this.fullname = fullname;
        this.address = address;
        this.email = email;
        this.rollNo = rollNo;
        this.age = age;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getRollNo() {
        return rollNo;
    }

    public void setRollNo(String rollNo) {
        this.rollNo = rollNo;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" + "fullname=" + fullname + ", address=" + address + ", email=" + email + ", rollNo=" + rollNo + ", age=" + age + '}';
    }
    
    public void display() {
        System.out.println(this);
    }
}




Đỗ Văn Huấn [T1907A]
Đỗ Văn Huấn

2020-04-16 04:51:14



<?xml version="1.0" encoding="UTF-8"?>
	<studentList>
		<student>
			<fullname>Do Van Huan</fullname>
			<age>19</age>
			<address>Bac Giang</address>
			<email>huandv2k1@gmail.com</email>
			<roll_no>A1</roll_no>
		</student>
		<student>
			<fullname>Ngo Thu Ha</fullname>
			<age>19</age>
			<address>Bac Giang</address>
			<email>ngothuha1401@gmail.com</email>
			<roll_no>A1</roll_no>
		</student>
	</studentList>



package java_XML.BaiTapNgay15_4_2020.bai1;

import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        try {
            // đọc dữ liệu từ file
            FileInputStream fis = new FileInputStream("XML_JSON/information2.xml");
            SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
            SAXParser saxParser = saxParserFactory.newSAXParser();

            StudentHandler stdhandler = new StudentHandler();
            saxParser.parse(fis, stdhandler);       // đọc dữ liệu được sử lý theo class StudentHandler

            stdhandler.output();  // hiển thị dữ liệu ra màn hình

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



package java_XML.BaiTapNgay15_4_2020.bai1;

public class Student {
    //Khai báo các thuộc tính của đối tượng
    String fullname, address, email, rollNo;
    int age;

    // tạo các hàm tạo
    public Student() {
    }

    public Student(String fullname, String address, String email, String rollNo, int age) {
        this.fullname = fullname;
        this.address = address;
        this.email = email;
        this.rollNo = rollNo;
        this.age = age;
    }



    // tạo các bộ get/set để lấy giá trị và gán giá trị.
    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getRollNo() {
        return rollNo;
    }

    public void setRollNo(String rollNo) {
        this.rollNo = rollNo;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {       // hàm hiển thị toString
        return "Student{" +
                "fullname='" + fullname + '\'' +
                ", address='" + address + '\'' +
                ", email='" + email + '\'' +
                ", rollNo='" + rollNo + '\'' +
                ", age=" + age +
                '}';
    }

    public void display() {
        System.out.println(toString());         // hiển thị các thuộc tính ra màn hình ( có thể thay toString thành this)
    }
}



package java_XML.BaiTapNgay15_4_2020.bai1;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;
import java.util.List;

public class StudentHandler extends DefaultHandler {
    List<Student> list = new ArrayList<>();         // khai báo 1 chuỗi Student
    Student std = null;                             // khai báo đối tượng student


    //tạo các biến dạng đúng sai để  gán giá trị lần lượt
    boolean isfullname = false, isage = false, isaddress = false, isemail = false, isrollno = false;


   // đọc dữ liệu từ file xml
    // đọc thẻ mở
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("student")) {           //khi dọc đến  thẻ có tên student
            std = new Student();                                       // thì tạo ra 1 student
        } else if (qName.equalsIgnoreCase("fullname")) {      //khi dọc đến  thẻ có tên fullname
            isfullname = true;
        } else if (qName.equalsIgnoreCase("age")) {            //khi dọc đến  thẻ có tên fullname
            isage = true;
        } else if (qName.equalsIgnoreCase("address")) {           //khi dọc đến  thẻ có tên fullname
            isaddress = true;
        } else if (qName.equalsIgnoreCase("email")) {             //khi dọc đến  thẻ có tên email
            isemail = true;
        } else if (qName.equalsIgnoreCase("roll_no")) {              //khi dọc đến  thẻ có tên roll_no
            isrollno = true;
        }
    }

    // dọc thẻ đóng
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("student")) {               // đọc đến thẻ đóng có tên student
            list.add(std);                                     // thì add giá trị của thằng student vào chuỗi vừa tạo ở trên
            std = null;                                        // add xong thì khai báo lại student = null
        } else if (qName.equalsIgnoreCase("fullname")) {
            isfullname = false;
        } else if (qName.equalsIgnoreCase("age")) {
            isage = false;
        } else if (qName.equalsIgnoreCase("address")) {
            isaddress = false;
        } else if (qName.equalsIgnoreCase("email")) {
            isemail = false;
        } else if (qName.equalsIgnoreCase("roll_no")) {
            isrollno = false;
        }
    }

    // lấy giấ trị đọc được và gán vào  biến của đối tượng
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String value = new String(ch, start, length);              // giá trị đọc được từ file
        if (std != null) {
            if (isfullname) {               // nếu đọc đến thẻ mở có tên fullname trong khi các thẻ khác chưa được đọc
                std.setFullname(value);        // thì lấy giá trị của thẻ và gán vào Fullname của thằng Student
            } else if (isage) {
                std.setAge(Integer.parseInt(value));       // gán dạng số nguyên.
            } else if (isaddress) {
                std.setAddress(value);
            } else if (isemail) {
                std.setEmail(value);
            } else if (isrollno) {
                std.setRollNo(value);
            }
        }
    }

    public void output() {
        for (Student s : list) {     // vòng for chạy qua các tất cả các thằng student
            s.display();               // thục hiện hiển thị thông tin của các thằng student.
        }
    }
}



Đường Thanh Bình [T1907A]
Đường Thanh Bình

2020-04-15 15:19:09



/*
 * 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 AptechFpt;

import aptech.Student;
import aptech.StudentHandler;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;

/**
 *
 * @author Administrator
 */
public class Main {
     public static void main(String[] args) throws SAXException, IOException {
        try {
            // TODO code application logic here
            File file = new File("student.xml");
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            
            StudentHandler studentHandler = new StudentHandler();
            
            parser.parse(file, studentHandler);
            
            //lay dc data
            List<Student> studentList = studentHandler.getStudentList();
            
            for (Student student : studentList) {
                student.display();
            }
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}




/*
 * 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 AptechFpt;

/**
 *
 * @author Administrator
 */
public class student {
     String fullname,address,email,rollNo;
    int age;

    public student() {
    }

    public student(String fullname, String address, String email, String rollNo, int age) {
        this.fullname = fullname;
        this.address = address;
        this.email = email;
        this.rollNo = rollNo;
        this.age = age;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getRollNo() {
        return rollNo;
    }

    public void setRollNo(String rollNo) {
        this.rollNo = rollNo;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" + "fullname=" + fullname + ", address=" + address + ", email=" + email + ", rollNo=" + rollNo + ", age=" + age + '}';
    }
    public void display() {
        System.out.println(this);
     }
    
}



/*
 * 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 AptechFpt;

import aptech.Student;
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 studentHandler extends DefaultHandler {

    static void display() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
     List<Student> studentList;
    boolean isFullname = false, isAge = false, isEmail = false, isAddress = false, isRollNo = false;
    Student currentStudent;

    public studentHandler() {
        studentList = new ArrayList<>();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("Student")) {
            currentStudent = new Student();
        } else if (qName.equalsIgnoreCase("fullname")) {
            isFullname = true;
        } else if (qName.equalsIgnoreCase("Age")) {
            isAge = true;
        } else if (qName.equalsIgnoreCase("Email")) {
            isEmail = true;
        } else if (qName.equalsIgnoreCase("Address")) {
            isAddress = true;
        } else if (qName.equalsIgnoreCase("Roll_No")) {
            isRollNo = true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("Student")) {
            studentList.add(currentStudent);
            currentStudent = null;
        } else if (qName.equalsIgnoreCase("fullname")) {
            isFullname = false;
        } else if (qName.equalsIgnoreCase("Age")) {
            isAge = false;
        } else if (qName.equalsIgnoreCase("Email")) {
            isEmail = false;
        } else if (qName.equalsIgnoreCase("Address")) {
            isAddress = false;
        } else if (qName.equalsIgnoreCase("Roll_No")) {
            isRollNo = false;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (currentStudent != null) {
            if (isFullname) {
                currentStudent.setFullname(new String(ch, start, length));
            } else if (isAge) {
                String ageStr = new String(ch, start, length);
                currentStudent.setAge(Integer.parseInt(ageStr));
            } else if (isEmail) {
                currentStudent.setEmail(new String(ch, start, length));
            } else if (isAddress) {
                currentStudent.setAddress(new String(ch, start, length));
            } else if (isRollNo) {
                currentStudent.setRollNo(new String(ch, start, length));
            }
        }
    }

    public List<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }
    
}



<?xml version="1.0" encoding="UTF-8"?>
<root>
	<studentList>
	    <student>
		     <fullname>Duong Thanh Binh</fullname>
		     <age>18</age>
		     <address>Ha Noi</address>
		     <email>db123@gmail.com</email>
		     <roll_no>R001</roll_no>
	    </student>
	     <student>
		     <fullname>Duong Thanh A</fullname>
		     <age>18</age>
		     <address>Ha Noi</address>
		     <email>db134@gmail.com</email>
		     <roll_no>R001</roll_no>
	    </student>
	</studentList>
</root>



Trần Anh Quân [T1907A]
Trần Anh Quân

2020-04-15 14:01: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 aaptech;

/**
 *
 * @author Anh Quan
 */
public class Student {
    String fullname, address, email, rollNo;
    int age;
    public Student(){
}
    public Student(String fullname, String address, String email, String rollNo, int age){
        this.fullname = fullname;
        this.address = address;
        this.email = email;
        this.rollNo = rollNo;
        this.age = age; 
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getRollNo() {
        return rollNo;
    }

    public void setRollNo(String rollNo) {
        this.rollNo = rollNo;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student{" + "fullname=" + fullname + ", address=" + address + ", email=" + email + ", rollNo=" + rollNo + ", age=" + age + '}';
    }
    
    public void display() {
        System.out.println(this);
    }
}
    



/*
 * 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 aaptech;
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 StudentHandler extends DefaultHandler {

    List<Student> studentList;
    boolean isFullname = false, isAge = false, isEmail = false, isAddress = false, isRollNo = false;
    Student currentStudent;

    public StudentHandler() {
        studentList = new ArrayList<>();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("Student")) {
            currentStudent = new Student();
        } else if (qName.equalsIgnoreCase("fullname")) {
            isFullname = true;
        } else if (qName.equalsIgnoreCase("Age")) {
            isAge = true;
        } else if (qName.equalsIgnoreCase("Email")) {
            isEmail = true;
        } else if (qName.equalsIgnoreCase("Address")) {
            isAddress = true;
        } else if (qName.equalsIgnoreCase("Roll_No")) {
            isRollNo = true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("Student")) {
            studentList.add(currentStudent);
            currentStudent = null;
        } else if (qName.equalsIgnoreCase("fullname")) {
            isFullname = false;
        } else if (qName.equalsIgnoreCase("Age")) {
            isAge = false;
        } else if (qName.equalsIgnoreCase("Email")) {
            isEmail = false;
        } else if (qName.equalsIgnoreCase("Address")) {
            isAddress = false;
        } else if (qName.equalsIgnoreCase("Roll_No")) {
            isRollNo = false;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (currentStudent != null) {
            if (isFullname) {
                currentStudent.setFullname(new String(ch, start, length));
            } else if (isAge) {
                String ageStr = new String(ch, start, length);
                currentStudent.setAge(Integer.parseInt(ageStr));
            } else if (isEmail) {
                currentStudent.setEmail(new String(ch, start, length));
            } else if (isAddress) {
                currentStudent.setAddress(new String(ch, start, length));
            } else if (isRollNo) {
                currentStudent.setRollNo(new String(ch, start, length));
            }
        }
    }

    public List<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }
}



/*
 * 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 aaptech;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;

/**
 *
 * @author Anh Quan
 */
public class Main {

    public static void main(String[] args) {
        try {
            // TODO code application logic here
            File file = new File("student.xml");
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            
            StudentHandler studentHandler = new StudentHandler();
            
            parser.parse(file, studentHandler);
            
            //lay dc data
            List<Student> studentList = studentHandler.getStudentList();
            
            for (Student student : studentList) {
                student.display();
            }
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}



<?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.
-->


<student_list>
    <student>
        <fullname>Tran Anh Quan</fullname>
        <age>19</age>
        <address>Ha Tinh</address>
        <email>tranquan09102001@gmail.com</email>
        <rollNo>R001</rollNo>
    </student>
    <student>
        <fullname>Tran Anh Tu</fullname>
        <age>20</age>
        <address>Ha Noi</address>
        <email>Trantu97@gmail.com</email>
        <rollNo>R001</rollNo>
    </student>
</student_list>



hoangduyminh [T1907A]
hoangduyminh

2020-04-15 11:34:55



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;

/**
 *
 * @author Minh
 */
public class Main {

    public static void main(String[] args) {
        try {
            // TODO code application logic here
            File file = new File("student.xml");
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            
            StudentHandler studentHandler = new StudentHandler();
            
            parser.parse(file, studentHandler);
            
            //lay dc data
            List<Student> studentList = studentHandler.getStudentList();
            
            for (Student student : studentList) {
                student.display();
            }
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}



/*
 * 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 Minh
 */
public class Student {
    String fullname,address,email,rollNo;
    int age;

    public Student() {
    }

    public Student(String fullname, String address, String email, String rollNo, int age) {
        this.fullname = fullname;
        this.address = address;
        this.email = email;
        this.rollNo = rollNo;
        this.age = age;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getRollNo() {
        return rollNo;
    }

    public void setRollNo(String rollNo) {
        this.rollNo = rollNo;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" + "fullname=" + fullname + ", address=" + address + ", email=" + email + ", rollNo=" + rollNo + ", age=" + age + '}';
    }
    public void display() {
        System.out.println(this);
     }
    
}



/*
 * 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 Minh
 */
public class StudentHandler extends DefaultHandler{
    List<Student> studentList;
    boolean isFullname = false, isAge = false, isEmail = false, isAddress = false, isRollNo = false;
    Student currentStudent;

    public StudentHandler() {
        studentList = new ArrayList<>();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("Student")) {
            currentStudent = new Student();
        } else if (qName.equalsIgnoreCase("fullname")) {
            isFullname = true;
        } else if (qName.equalsIgnoreCase("Age")) {
            isAge = true;
        } else if (qName.equalsIgnoreCase("Email")) {
            isEmail = true;
        } else if (qName.equalsIgnoreCase("Address")) {
            isAddress = true;
        } else if (qName.equalsIgnoreCase("Roll_No")) {
            isRollNo = true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("Student")) {
            studentList.add(currentStudent);
            currentStudent = null;
        } else if (qName.equalsIgnoreCase("fullname")) {
            isFullname = false;
        } else if (qName.equalsIgnoreCase("Age")) {
            isAge = false;
        } else if (qName.equalsIgnoreCase("Email")) {
            isEmail = false;
        } else if (qName.equalsIgnoreCase("Address")) {
            isAddress = false;
        } else if (qName.equalsIgnoreCase("Roll_No")) {
            isRollNo = false;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (currentStudent != null) {
            if (isFullname) {
                currentStudent.setFullname(new String(ch, start, length));
            } else if (isAge) {
                String ageStr = new String(ch, start, length);
                currentStudent.setAge(Integer.parseInt(ageStr));
            } else if (isEmail) {
                currentStudent.setEmail(new String(ch, start, length));
            } else if (isAddress) {
                currentStudent.setAddress(new String(ch, start, length));
            } else if (isRollNo) {
                currentStudent.setRollNo(new String(ch, start, length));
            }
        }
    }

    public List<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }
  }



<?xml version="1.0" encoding="UTF-8"?>

<root>
	<studentList>
		<student>
			<fullname>Hoang Duy Minh</fullname>
			<age>22</age>
			<address>Ha Noi</address>
			<email>minh@gmail.com</email>
			<roll_no>R001</roll_no>
		</student>
		<student>
			<fullname>Hoang Duy Hieu</fullname>
			<age>22</age>
			<address>Ha Noi</address>
			<email>hieu@gmail.com</email>
			<roll_no>R001</roll_no>
		</student>
	</studentList>
</root>



Lê Minh Bắc [T1907A]
Lê Minh Bắc

2020-04-15 10:21:54



<?xml version="1.0" encoding="UTF-8"?>


<student_list>
      <student>
              <fullname>Le Minh Bac</fullname>
              <age>19</age>
              <address>Ha Noi</address>
              <email>leminhbac2001@gmail.com</email>
              <roll_no>123</roll_no>
      </student>
      <student>
              <fullname>Le Ngoc Nhi</fullname>
              <age>19</age>
              <address>Ha Noi</address>
              <email>lengocnhi2001@gmail.com</email>
              <roll_no>122</roll_no>
      </student>
</student_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.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;

/**
 *
 * @author lemin
 */
public class Main {
    public static void main(String[] agrs) throws SAXException {
        try {
            File file = new File("student.xml");
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            
            StudentHandler studentHandler = new StudentHandler();
            
            parser.parse(file, studentHandler);

            List<student> studentList = studentHandler.getStudentList();

            for (student std : studentList) {
                std.display();
            }
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}



/*
 * 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 lemin
 */
public class StudentHandler extends DefaultHandler {

    List<student> studentList;
    boolean isFullname = false, isAge = false, isEmail = false, isAddress = false, isRollNo = false;
    student currentStudent;

    public StudentHandler() {
        studentList = new ArrayList<>();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("Student")) {
            currentStudent = new student();
        } else if (qName.equalsIgnoreCase("fullname")) {
            isFullname = true;
        } else if (qName.equalsIgnoreCase("Age")) {
            isAge = true;
        } else if (qName.equalsIgnoreCase("Email")) {
            isEmail = true;
        } else if (qName.equalsIgnoreCase("Address")) {
            isAddress = true;
        } else if (qName.equalsIgnoreCase("Roll_No")) {
            isRollNo = true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("Student")) {
            studentList.add(currentStudent);
            currentStudent = null;
        } else if (qName.equalsIgnoreCase("fullname")) {
            isFullname = false;
        } else if (qName.equalsIgnoreCase("Age")) {
            isAge = false;
        } else if (qName.equalsIgnoreCase("Email")) {
            isEmail = false;
        } else if (qName.equalsIgnoreCase("Address")) {
            isAddress = false;
        } else if (qName.equalsIgnoreCase("Roll_No")) {
            isRollNo = false;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (currentStudent != null) {
            if (isFullname) {
                currentStudent.setFullname(new String(ch, start, length));
            } else if (isAge) {
                String ageStr = new String(ch, start, length);
                currentStudent.setAge(Integer.parseInt(ageStr));
            } else if (isEmail) {
                currentStudent.setEmail(new String(ch, start, length));
            } else if (isAddress) {
                currentStudent.setAddress(new String(ch, start, length));
            } else if (isRollNo) {
                currentStudent.setRollNo(new String(ch, start, length));
            }
            
        }
        
    }
    

    public List<student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<student> studentList) {
        this.studentList = studentList;
    }
}



/*
 * 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 lemin
 */
public class student {
    String fullname, address, email, rollNo;
    int age;

    public student() {
    }

    public student(String fullname, String address, String email, String rollNo, int age) {
        this.fullname = fullname;
        this.address = address;
        this.email = email;
        this.rollNo = rollNo;
        this.age = age;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getRollNo() {
        return rollNo;
    }

    public void setRollNo(String rollNo) {
        this.rollNo = rollNo;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" + "fullname=" + fullname + ", address=" + address + ", email=" + email + ", rollNo=" + rollNo + ", age=" + age + '}';
    }
    
    public void display() {
        System.out.println(this);
    }
}



Trương Công Vinh [T1907A]
Trương Công Vinh

2020-04-15 09:54:41



/*
 * 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 Lesion2.Handler;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;

/**
 *
 * @author DELL
 */
public class mail {
    public static void main(String[] args) throws FileNotFoundException, ParserConfigurationException, SAXException, IOException {
        FileInputStream fis = new FileInputStream("d://XML/LessionXML_Handler.xml");
        
        SAXParserFactory factory = SAXParserFactory.newInstance();
	SAXParser saxParser = factory.newSAXParser();
        studentHandler handler = new studentHandler();
        saxParser.parse(fis, handler);
        handler.display();
    }
}



package Lesion2.Handler;

/*
 * 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.
 */

/**
 *
 * @author DELL
 */
public class student {
    String fullname, address ,email,roll_no;
    int age ;

    public student() {
    }

    public student(String fullname, String address, String email, String roll_no, int age) {
        this.fullname = fullname;
        this.address = address;
        this.email = email;
        this.roll_no = roll_no;
        this.age = age;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getRoll_no() {
        return roll_no;
    }

    public void setRoll_no(String roll_no) {
        this.roll_no = roll_no;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "student{" + "fullname=" + fullname + ", address=" + address + ", email=" + email + ", roll_no=" + roll_no + ", age=" + age + '}';
    }
    public void display(){
        System.out.println(toString());
    }
}



/*
 * 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 Lesion2.Handler;

import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author DELL
 */
public class studentHandler extends DefaultHandler {

    ArrayList<student> stdList;
    student currentStudent = null;

    boolean isFullname = false, isAge = false, isEmail = false, isAddress = false, isRollNo = false;

    public studentHandler() {
        stdList = new ArrayList<>();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("student")) {
            currentStudent = new student();
        }else if(qName.equalsIgnoreCase("fullname")){
            isFullname = true;
        }else if(qName.equalsIgnoreCase("age")){
            isAge = true;
        }else if(qName.equalsIgnoreCase("address")){
            isAddress = true;
        }else if(qName.equalsIgnoreCase("email")){
            isEmail = true;
        }else if(qName.equalsIgnoreCase("roll_no")){
            isRollNo = true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("student")) {
            stdList.add(currentStudent);
            currentStudent = null;
        }else if(qName.equalsIgnoreCase("fullname")){
            isFullname = false;
        }else if(qName.equalsIgnoreCase("age")){
            isAge = false;
        }else if(qName.equalsIgnoreCase("address")){
            isAddress = false;
        }else if(qName.equalsIgnoreCase("email")){
            isEmail = false;
        }else if(qName.equalsIgnoreCase("roll_no")){
            isRollNo = false;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String value = new String(ch, start, length);
        if (currentStudent != null) {
            if (isFullname) {
                currentStudent.setFullname(value);
            }else if (isAge) {
                currentStudent.setAge(Integer.parseInt(value));
            }else if (isAddress) {
                currentStudent.setAddress(value);
            }else if (isEmail) {
                currentStudent.setEmail(value);
            }else if (isRollNo) {
                currentStudent.setRoll_no(value);
            }
        }
    }

    public ArrayList<student> getStdList() {
        return stdList;
    }

    public void setStdList(ArrayList<student> stdList) {
        this.stdList = stdList;
    }
    
public void display(){
    for (student object : stdList) {
        object.display();
    }
}
}



Minh Nghia [T1907A]
Minh Nghia

2020-04-15 09:52:47



/*
 * 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 javaxml;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;

/**
 *
 * @author Administrator
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        try {
            File file = new File("studentx.xml");
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            
            StudentHandler studentHandler = new StudentHandler();
            parser.parse(file, studentHandler);
            
            //lay dc data
            List<Student> studentList = studentHandler.getStudentList();
            
            for (Student student : studentList) {
                student.display();
            }
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}



/*
 * 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 javaxml;

/**
 *
 * @author Administrator
 */
public class Student {
    String fullname, address , email,rollNo;
    int age;
    public Student() {
    }

    public Student(String fullname, String address, String email, String rollNo, int age) {
        this.fullname = fullname;
        this.address = address;
        this.email = email;
        this.rollNo = rollNo;
        this.age = age;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getRollNo() {
        return rollNo;
    }

    public void setRollNo(String rollNo) {
        this.rollNo = rollNo;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" + "fullname=" + fullname + ", address=" + address + ", email=" + email + ", rollNo=" + rollNo + ", age=" + age + '}';
    }
    public void display(){
        System.out.println(this);
    }
   
    
}



/*
 * 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 javaxml;

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 StudentHandler extends DefaultHandler {

    List<Student> studentList;
    boolean isFullname = false;
    boolean isAge = false;
    boolean isEmail = false;
    boolean isAddress = false;
    boolean isRollNo = false;
    Student currentStudent;

    public StudentHandler() {
        studentList = new ArrayList<>();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("Student")) {
            currentStudent = new Student();
        } else if (qName.equalsIgnoreCase("fullname")) {
            isFullname = true;

        } else if (qName.equalsIgnoreCase("Age")) {
            isAge = true;

        } else if (qName.equalsIgnoreCase("Email")) {
            isEmail = true;

        } else if (qName.equalsIgnoreCase("Address")) {
            isAddress = true;

        } else if (qName.equalsIgnoreCase("Roll_No")) {
            isRollNo = true;

        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("Student")) {
            studentList.add(currentStudent);
            currentStudent = null;
        } else if (qName.equalsIgnoreCase("fullname")) {
            isFullname = false;

        } else if (qName.equalsIgnoreCase("Age")) {
            isAge = false;

        } else if (qName.equalsIgnoreCase("Email")) {
            isEmail = false;

        } else if (qName.equalsIgnoreCase("Address")) {
            isAddress = false;

        } else if (qName.equalsIgnoreCase("Roll_No")) {
            isRollNo = false;

        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (currentStudent != null) {
            if (isFullname) {
                currentStudent.setFullname(new String(ch, start, length));
            } else if (isAge) {
                String ageStr = new String(ch, start, length);
                currentStudent.setAge(Integer.parseInt(ageStr));
            } else if (isEmail) {
                currentStudent.setEmail(new String(ch, start, length));
            } else if (isAddress) {
                currentStudent.setAddress(new String(ch, start, length));
            } else if (isRollNo) {
                currentStudent.setRollNo(new String(ch, start, length));
            }
        }
        
    }

    public List<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }

}



Phí Văn Long [T1907A]
Phí Văn Long

2020-04-15 09:51:35



/*
 * 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 Test;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;

/**
 *
 * @author Admin
 */
public class Main {
     public static void main(String[] args) {
      
         try {
             File file = new File("student.xml");
             SAXParserFactory factory = SAXParserFactory.newInstance();
             SAXParser parser = factory.newSAXParser();
             
             StudentHandler studentHandler = new StudentHandler();
             
             parser.parse(file, studentHandler);
             
             //da lay duoc data ra
             List<Student> studentList = studentHandler.getStudentList();
             for(Student student : studentList){
                 student.display();
             }
         } catch (ParserConfigurationException ex) {
             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
         } catch (SAXException ex) {
             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
         } catch (IOException ex) {
             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
         }
            
}
}



/*
 * 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 Test;

/**
 *
 * @author Admin
 */
public class Student {
    String fullname,address,email,rollNo;
    int age;

    public Student() {
    }

    public Student(String fullname, String address, String email, String rollNo, int age) {
        this.fullname = fullname;
        this.address = address;
        this.email = email;
        this.rollNo = rollNo;
        this.age = age;
    }
     public String getRollNo() {
        return rollNo;
    }

    public void setRollNo(String rollNo) {
        this.rollNo = rollNo;
    }
    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

 

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" + " rollNo = " + rollNo + ", fullname = " + fullname + ", address = " + address + ", email = " + email  + ", age = " + age + '}';
    }
    
    public void display(){
        System.out.println(this);
    }
}



/*
 * 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 Test;

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 StudentHandler extends DefaultHandler{
     List<Student> studentList;
    boolean isFullname = false, isAge = false, isEmail = false, isAddress = false, isRollNo = false;
    Student currentStudent;

    public StudentHandler() {
        studentList = new ArrayList<>();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("Student")) {
            currentStudent = new Student();
        }else if (qName.equalsIgnoreCase("Roll_No")) {
            isRollNo = true;
        }
        else if (qName.equalsIgnoreCase("fullname")) {
            isFullname = true;
        } else if (qName.equalsIgnoreCase("Age")) {
            isAge = true;
        } else if (qName.equalsIgnoreCase("Email")) {
            isEmail = true;
        } else if (qName.equalsIgnoreCase("Address")) {
            isAddress = true;
        } 
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("Student")) {
            studentList.add(currentStudent);
            currentStudent = null;
        } else if (qName.equalsIgnoreCase("Roll_No")) {
            isRollNo = false;
        }else if (qName.equalsIgnoreCase("fullname")) {
            isFullname = false;
        } else if (qName.equalsIgnoreCase("Age")) {
            isAge = false;
        } else if (qName.equalsIgnoreCase("Email")) {
            isEmail = false;
        } else if (qName.equalsIgnoreCase("Address")) {
            isAddress = false;
        } 
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (currentStudent != null) {
            if (isFullname) {
                currentStudent.setFullname(new String(ch, start, length));
            } else if (isRollNo) {
                currentStudent.setRollNo(new String(ch, start, length));
            }else if (isAge) {
                String ageStr = new String(ch, start, length);
                currentStudent.setAge(Integer.parseInt(ageStr));
            } else if (isEmail) {
                currentStudent.setEmail(new String(ch, start, length));
            } else if (isAddress) {
                currentStudent.setAddress(new String(ch, start, length));
            } 
        }
    }

    public List<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }
}



NguyenHuuThanh [T1907A]
NguyenHuuThanh

2020-04-15 09:31:10



/*
 * 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 abc
 */
public class Student {
    String fullname, address, email, rollNo;
    int age;

    public Student() {
    }

    public Student(String fullname, String address, String email, String rollNo, int age) {
        this.fullname = fullname;
        this.address = address;
        this.email = email;
        this.rollNo = rollNo;
        this.age = age;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getRollNo() {
        return rollNo;
    }

    public void setRollNo(String rollNo) {
        this.rollNo = rollNo;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" + "fullname=" + fullname + ", address=" + address + ", email=" + email + ", rollNo=" + rollNo + ", age=" + age + '}';
    }
    
    public void display() {
        System.out.println(this);
    }
}

/*
 * 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 abc
 */
public class StudentHandler extends DefaultHandler {

    List<Student> studentList;
    boolean isFullname = false, isAge = false, isEmail = false, isAddress = false, isRollNo = false;
    Student currentStudent;

    public StudentHandler() {
        studentList = new ArrayList<>();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("Student")) {
            currentStudent = new Student();
        } else if (qName.equalsIgnoreCase("fullname")) {
            isFullname = true;
        } else if (qName.equalsIgnoreCase("Age")) {
            isAge = true;
        } else if (qName.equalsIgnoreCase("Email")) {
            isEmail = true;
        } else if (qName.equalsIgnoreCase("Address")) {
            isAddress = true;
        } else if (qName.equalsIgnoreCase("Roll_No")) {
            isRollNo = true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("Student")) {
            studentList.add(currentStudent);
            currentStudent = null;
        } else if (qName.equalsIgnoreCase("fullname")) {
            isFullname = false;
        } else if (qName.equalsIgnoreCase("Age")) {
            isAge = false;
        } else if (qName.equalsIgnoreCase("Email")) {
            isEmail = false;
        } else if (qName.equalsIgnoreCase("Address")) {
            isAddress = false;
        } else if (qName.equalsIgnoreCase("Roll_No")) {
            isRollNo = false;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (currentStudent != null) {
            if (isFullname) {
                currentStudent.setFullname(new String(ch, start, length));
            } else if (isAge) {
                String ageStr = new String(ch, start, length);
                currentStudent.setAge(Integer.parseInt(ageStr));
            } else if (isEmail) {
                currentStudent.setEmail(new String(ch, start, length));
            } else if (isAddress) {
                currentStudent.setAddress(new String(ch, start, length));
            } else if (isRollNo) {
                currentStudent.setRollNo(new String(ch, start, length));
            }
        }
    }

    public List<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }
}

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;

/**
 *
 * @author abc
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            // TODO code application logic here
            File file = new File("student.xml");
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            
            StudentHandler studentHandler = new StudentHandler();
            
            parser.parse(file, studentHandler);
            
            //lay dc data
            List<Student> studentList = studentHandler.getStudentList();
            
            for (Student student : studentList) {
                student.display();
            }
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}