By GokiSoft.com| 19:48 10/04/2023|
Java Advanced

XML: Viết chương trình phân tích tài liệu XML thông tin cá nhân bằng Java

Viết tài liệu XML mô tả thông tin cá nhân, sở thích, dụng cụ học tập, ngôn ngữ lập trình bạn thích

Yêu cầu: Khi đọc tài liệu có thể biết được profile của 1 cá nhân duy nhất, xem được thông tin cá nhân, sở thích, ngôn ngữ lập trình của cá nhân đó và các trang thiết bị học tập. Các thẻ phải mô tả chính xác nội dung đang cần diễn đạt (cách đặt tên)

Viết chương trình phân tích tài liệu trên bằng Java

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

5

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

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

2020-04-16 13:17:08



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

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("info.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();
        
        ProfileHandler profileHandler = new ProfileHandler();
        
        saxParser.parse(fis, profileHandler);
        //hiển thị
        profileHandler.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 btXML;

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 Nong
 */
public class ProfileHandler extends DefaultHandler {

    List<Profile> ProList = new ArrayList<>();
    Profile Pro = null;

    boolean isFullname = false;
    boolean isAge = false;
    boolean isEmail = false;
    boolean isAddress = false;
    boolean isPhoneNumber = false;
    boolean isHeight = false;
    boolean isWeight = false;
    boolean isStatus = false;
    boolean isHobbies = false;
    boolean isTools = false;
    boolean isLanguage = false;

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("profile")) {
            Pro = new Profile();
        } 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("PhoneNumber")) {
            isPhoneNumber = true;
        } else if (qName.equalsIgnoreCase("Height")) {
            isHeight = true;
        } else if (qName.equalsIgnoreCase("Weight")) {
            isWeight = true;
        } else if (qName.equalsIgnoreCase("Status")) {
            isStatus = true;
        } else if (qName.equalsIgnoreCase("Hobbies")) {
            isHobbies = true;
        } else if (qName.equalsIgnoreCase("tool")) {
            isTools = true;
        } else if (qName.equalsIgnoreCase("Language")) {
            isLanguage = true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("profile")) {
            ProList.add(Pro);
            Pro = 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("PhoneNumber")) {
            isPhoneNumber = false;
        } else if (qName.equalsIgnoreCase("Height")) {
            isHeight = false;
        } else if (qName.equalsIgnoreCase("Weight")) {
            isWeight = false;
        } else if (qName.equalsIgnoreCase("Status")) {
            isStatus = false;
        } else if (qName.equalsIgnoreCase("Hobbies")) {
            isHobbies = false;
        } else if (qName.equalsIgnoreCase("tool")) { // chỗ này nè: 1
            
            isTools = false;
        } else if (qName.equalsIgnoreCase("Language")) {
            isLanguage = false;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String value = new String(ch, start, length);
        if (isFullname) {
            Pro.setFullname(value);
        } else if (isAge) {
            Pro.setAge(Integer.parseInt(value));
        } else if (isEmail) {
            Pro.setEmail(value);
        } else if (isAddress) {
            Pro.setAddress(value);
        } else if (isPhoneNumber) {
            Pro.setPhonenumber(Integer.parseInt(value));
        } else if (isHeight) {
            Pro.setHeight(value);
        } else if (isWeight) {
            Pro.setWeight(value);
        } else if (isStatus) {
            Pro.setStatus(value);
        } else if (isHobbies) {
            Pro.getHobbiesList().add(value);
        } else if (isTools) {
            Pro.getToolList().add(value);
        } else if (isLanguage) {
            Pro.getProLanguageList().add(value);
        }
    }

    public void display() {
        for (Profile profile9 : ProList) {
            profile9.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 btXML;

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

/**
 *
 * @author Nong
 */
public class Profile {
    String fullname,email,address,height,weight,status;
    int age,phonenumber;
    List<String> toolList = new ArrayList<>();
    List<String> hobbiesList = new ArrayList<>();
    List<String> proLanguageList = new ArrayList<>();

    public Profile() {
    }

    public String getFullname() {
        return fullname;
    }

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

    public String getEmail() {
        return email;
    }

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

    public String getAddress() {
        return address;
    }

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

    public String getHeight() {
        return height;
    }

    public void setHeight(String height) {
        this.height = height;
    }

    public String getWeight() {
        return weight;
    }

    public void setWeight(String weight) {
        this.weight = weight;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public int getAge() {
        return age;
    }

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

    public int getPhonenumber() {
        return phonenumber;
    }

    public void setPhonenumber(int phonenumber) {
        this.phonenumber = phonenumber;
    }

    public List<String> getToolList() {
        return toolList;
    }

    public void setToolList(List<String> toolList) {
        this.toolList = toolList;
    }

    public List<String> getHobbiesList() {
        return hobbiesList;
    }

    public void setHobbiesList(List<String> hobbiesList) {
        this.hobbiesList = hobbiesList;
    }

    public List<String> getProLanguageList() {
        return proLanguageList;
    }

    public void setProLanguageList(List<String> proLanguageList) {
        this.proLanguageList = proLanguageList;
    }
    public void display(){
        System.out.println(" ->Thong tin ca nhan : ");
        System.out.format("Fullname : %s \nEmail : %s \nAddress : %s \nHeight : %s \nWeight : %s \nStatus : %s \nAge: %d \nPhoneNumber : %d",fullname ,email ,address  ,height  ,weight  ,status ,age ,phonenumber);
        System.out.println("\n ->Danh sach so thich: ");
        for (String hobbiesList9 : hobbiesList){
            System.out.println(hobbiesList9 + " ");
        }
        System.out.println("\n ->Danh sach cong cu : ");
        for (String toolList9 : toolList) {
            System.out.println(toolList9 + " ");
        }
        System.out.println("\n ->Danh sach ngon ngu lap trinh yeu thich : ");
        for (String prolanguage9 : proLanguageList ){
            System.out.println(prolanguage9 + " ");
        }
    } 
}



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

2020-04-16 13:01:22



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


<information>

  <person>

    <no>1</no>

    <fullname>truong cong vinh</fullname>

    <age>18</age>

    <gender>male</gender>

    <email>duongcongvinh18122001@gmail.com</email>

    <address>Ha noi</address>

    <favourite_list>
      <favourite>coding</favourite>
      <favourite>coding</favourite>
    </favourite_list>

    <learningtools_list>
      <tool>tool 1</tool>
      <tool>tool 2</tool>
      <tool>Laptop</tool>
    </learningtools_list>

    <programming_languages_you_like>
                <programming_language>@@</programming_language>
                <programming_language>@@</programming_language>
    </programming_languages_you_like>

  </person>
  <person>

    <no>1</no>

    <fullname>truong cong vinh</fullname>

    <age>18</age>

    <gender>male</gender>

    <email>duongcongvinh18122001@gmail.com</email>

    <address>Ha noi</address>

    <favourite_list>
      <favourite>coding</favourite>
      <favourite>coding</favourite>
    </favourite_list>

    <learningtools_list>
      <tool>tool 1</tool>
      <tool>tool 2</tool>
      <tool>Laptop</tool>
    </learningtools_list>

    <programming_languages_you_like>
                <programming_language>@@</programming_language>
                <programming_language>@@</programming_language>
    </programming_languages_you_like>

  </person>

</information>



/*
 * 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 Lession2.Favourite;

import java.util.ArrayList;

/**
 *
 * @author DELL
 */
public class person {
    String fullname,gender, address , email ;
    int age , no;
    ArrayList<String> favourite = new ArrayList<>();
    ArrayList<String> learningtool = new ArrayList<>();
    ArrayList<String> programming_language = new ArrayList<>();

    public person() {
    }

    public person(String fullname, String gender, String address, String email, int age, int no) {
        this.fullname = fullname;
        this.gender = gender;
        this.address = address;
        this.email = email;
        this.age = age;
        this.no = no;
    }

    public String getFullname() {
        return fullname;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    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;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public ArrayList<String> getFavourite() {
        return favourite;
    }

    public void setFavourite(ArrayList<String> favourite) {
        this.favourite = favourite;
    }

    public ArrayList<String> getLearningtool() {
        return learningtool;
    }

    public void setLearningtool(ArrayList<String> learningtool) {
        this.learningtool = learningtool;
    }

    public ArrayList<String> getProgramming_language() {
        return programming_language;
    }

    public void setProgramming_language(ArrayList<String> programming_language) {
        this.programming_language = programming_language;
    }

    @Override
    public String toString() {
        return "person{" + "fullname=" + fullname + ", gender=" + gender + ", address=" + address + ", email=" + email + ", age=" + age + ", no=" + no + '}';
    }

    public void display(){
        System.out.println(toString());
        System.out.println("Favourites :");
        for (int i = 0; i < favourite.size(); i++) {
            System.out.println("Favourite : "+favourite.get(i));
        }
        System.out.println("learningtools_list");
        for (int i = 0; i < learningtool.size(); i++) {
            System.out.println("tool : " + learningtool.get(i) );
        }
        System.out.println("programming_languages_you_like : ");
        for (int i = 0; i < programming_language.size(); i++) {
            System.out.println("programming_language : "+programming_language.get(i));
        }
    }
    
}



/*
 * 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 Lession2.Favourite;

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 DELL
 */
public class personHandler extends DefaultHandler {
    List<person> personList = new ArrayList<>();
    person currentPerson = null;
    boolean isNo = false ,isFullname = false ,isAge = false ,isGender = false ,isEmail = false ,isAddress = false ,
            isFavourite = false ,isTool = false ,isLanguage = false  ;

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("person")) {
            
            currentPerson = new person();
        }else if (qName.equalsIgnoreCase("no")) {
            isNo = true;
        }else if (qName.equalsIgnoreCase("fullname")) {
            isFullname = true;
        }else if (qName.equalsIgnoreCase("age")) {
            isAge = true;
        }else if (qName.equalsIgnoreCase("gender")) {
            isGender = true;
        }else if (qName.equalsIgnoreCase("email")) {
            isEmail = true;
        }else if (qName.equalsIgnoreCase("address")) {
            isAddress = true;
        }else if (qName.equalsIgnoreCase("favourite")) {
            isFavourite=true;
        }else if (qName.equalsIgnoreCase("tool")) {
            isTool = true;
        }else if (qName.equalsIgnoreCase("programming_language")) {
            isLanguage = true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("person")) {
            personList.add(currentPerson);
            currentPerson= null;
        }else if (qName.equalsIgnoreCase("no")) {
            isNo = false;
        }else if (qName.equalsIgnoreCase("fullname")) {
            isFullname = false;
        }else if (qName.equalsIgnoreCase("age")) {
            isAge = false;
        }else if (qName.equalsIgnoreCase("gender")) {
            isGender = false;
        }else if (qName.equalsIgnoreCase("email")) {
            isEmail = false;
        }else if (qName.equalsIgnoreCase("address")) {
            isAddress = false;
        }else if (qName.equalsIgnoreCase("favourite")) {
            
            isFavourite=false;
        }else if (qName.equalsIgnoreCase("tool")) {
            isTool = false;
        }else if (qName.equalsIgnoreCase("programming_language")) {
            isLanguage = false;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String value = new String(ch, start, length);
        if (currentPerson != null) {
            if (isNo) {
                currentPerson.setNo(Integer.parseInt(value));
            }else if (isFullname) {
                currentPerson.setFullname(value);
            }else if (isAge) {
                currentPerson.setAge(Integer.parseInt(value));
            }else if (isGender) {
                currentPerson.setGender(value);
            }else if (isEmail) {
                currentPerson.setEmail(value);
            }else if (isAddress) {
                currentPerson.setAddress(value);
            }else if (isFavourite) {
                
                currentPerson.getFavourite().add(value);
            }else if (isTool) {
               
                currentPerson.getLearningtool().add(value);
            }else if (isLanguage) {
                
                currentPerson.getProgramming_language().add(value);
            }
        }
    }
    public void display(){
        for (person object : personList) {
            object.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 Lession2.Favourite;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
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 DELL
 */
public class main {

    public static void main(String[] args) {
        FileInputStream fis = null;
        try {

             fis = new FileInputStream("d://XML/Lession1_test1b.xml");
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            
            personHandler handler = new personHandler();
            
            saxParser.parse(fis, handler);
            handler.display();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
        } 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);
        }finally{
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
         
        
    }
}



Trần Mạnh Dũng [T1907A]
Trần Mạnh Dũng

2020-04-16 10:43:37



<?xml version="1.0" encoding="UTF-8"?>
<myprofile>
    <name>Trần Mạnh Dũng</name>
    <age>18</age>
    <address>Phú Thọ</address>
    <like>Electronic Sport</like>
    <learningtools>Laptop</learningtools>
    <programminglanguage>Java</programminglanguage>
</myprofile>



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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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) throws Exception {
        FileInputStream fis = new FileInputStream("ProfileXML.xml");
        
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        ProfileHandler profileHandler = new ProfileHandler();  
    
        saxParser.parse(fis, profileHandler);
        
        profileHandler.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 Profile;

/**
 *
 * @author admin
 */
public class Profile {
    String name, address, like, learningtools, programminglanguage;
    int age;

    public Profile() {
    }

    public Profile(String name, String address, String like, String learningtools, String programminglanguage, int age) {
        this.name = name;
        this.address = address;
        this.like = like;
        this.learningtools = learningtools;
        this.programminglanguage = programminglanguage;
        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 String getLike() {
        return like;
    }

    public void setLike(String like) {
        this.like = like;
    }

    public String getLearningtools() {
        return learningtools;
    }

    public void setLearningtools(String learningtools) {
        this.learningtools = learningtools;
    }

    public String getProgramminglanguage() {
        return programminglanguage;
    }

    public void setProgramminglanguage(String programminglanguage) {
        this.programminglanguage = programminglanguage;
    }

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Profile{" + "name=" + name + ", address=" + address + ", like=" + like + ", learningtools=" + learningtools + ", programminglanguage=" + programminglanguage + ", 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 Profile;

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

    List<Profile> myprofileList = new ArrayList<>();
    boolean isName = false;
    boolean isAge = false;
    boolean isAddress = false;
    boolean isLike = false;
    boolean isLearningtools = false;
    boolean isProgramminglanguage = false;

    Profile currentProfile = null;

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("myprofile")) {
            currentProfile = new Profile();
        } else if (qName.equalsIgnoreCase("name")) {
            isName = true;
        } else if (qName.equalsIgnoreCase("age")) {
            isAge = true;
        } else if (qName.equalsIgnoreCase("address")) {
            isAddress = true;
        } else if (qName.equalsIgnoreCase("like")) {
            isLike = true;
        } else if (qName.equalsIgnoreCase("learningtools")) {
            isLearningtools = true;
        } else if (qName.equalsIgnoreCase("programminglanguage")) {
            isProgramminglanguage = true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("myprofile")) {
            myprofileList.add(currentProfile);
            currentProfile = null;
        } else if (qName.equalsIgnoreCase("name")) {
            isName = false;
        } else if (qName.equalsIgnoreCase("age")) {
            isAge = false;
        } else if (qName.equalsIgnoreCase("address")) {
            isAddress = false;
        } else if (qName.equalsIgnoreCase("like")) {
            isLike = false;
        } else if (qName.equalsIgnoreCase("learningtools")) {
            isLearningtools = false;
        } else if (qName.equalsIgnoreCase("programminglanguage")) {
            isProgramminglanguage = false;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String value = new String(ch, start, length);
        if (isName) {
            currentProfile.setName(value);
        } else if (isAge) {
            currentProfile.setAge(Integer.parseInt(value));
        } else if (isAddress) {
            currentProfile.setAddress(value);
        } else if (isLike) {
            currentProfile.setLike(value);
        } else if (isLearningtools) {
            currentProfile.setLearningtools(value);
        } else if (isProgramminglanguage) {
            currentProfile.setProgramminglanguage(value);
        }
        
    }

   public void display(){
       for (Profile profile : myprofileList) {
           profile.display();
       }
   }
    
}



thienphu [T1907A]
thienphu

2020-04-16 03:02:13



<?xml version="1.0" encoding="UTF-8"?>
	<information>
		<Person> 
			<rollNo>pp01</rollNo>
			<fullname>Do Thien Phu</fullname>
			<age>18</age>
			<address>Ha Noi</address>
			
			<!-- danh sach so thich -->
			<enjoyList>
					<enjoy>play soccer </enjoy>
					<enjoy>swimming</enjoy>
					<enjoy>gym</enjoy>
			</enjoyList>
			
			<!--cac ngon ngu lap trinh -->
			<programmingLanguageList>
					<programmingLanguage>Java</programmingLanguage>
					<programmingLanguage>PHP</programmingLanguage>
					
			</programmingLanguageList>
			
		</Person>
		
		<Person> 
			<rollNo>pp02</rollNo>
			<fullname>Nguyen Hong Ngoc</fullname>
			<age>21</age>
			<address>Ha Noi</address>
			
			<!-- danh sach so thich -->
			<enjoyList>
					<enjoy>readbook </enjoy>
					<enjoy>swimming</enjoy>
					<enjoy>gym</enjoy>
			</enjoyList>
			
			<!--cac ngon ngu lap trinh -->
			<programmingLanguageList>
					<programmingLanguage>Java</programmingLanguage>
					<programmingLanguage>PHP</programmingLanguage>
					
			</programmingLanguageList>
			
		</Person>
	</information>



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

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

/**
 *
 * @author Thien Phu
 */
public class Information {

    String rollNo, fullname, address;
    int age;
    List<String> enjoyList = new ArrayList<>();
    List<String> programmingLanguageList = new ArrayList<>();

    public Information() {
    }

    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 int getAge() {
        return age;
    }

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

    public List<String> getEnjoyList() {
        return enjoyList;
    }

    public void setEnjoyList(List<String> enjoyList) {
        this.enjoyList = enjoyList;
    }

    public List<String> getProgrammingLanguageList() {
        return programmingLanguageList;
    }

    public void setProgrammingLanguageList(List<String> programmingLanguageList) {
        this.programmingLanguageList = programmingLanguageList;
    }

    void display() {
        System.out.println("\nInformation:");
        System.out.println("rollNo = " + rollNo + " : fullname= " + fullname + " :Age= " + age + " :Address= " + address);
        System.out.println("List Enjoy: ");
        for (String enjoyList1 : enjoyList) {
            System.out.print(enjoyList1+"||\t");
        }
        System.out.println("\nProgrammingLanguageList: ");
        for (String pr : programmingLanguageList) {
            System.out.print(pr+" ||\t");
        }
    }

}



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

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 Thien Phu
 */
public class InformationHander extends DefaultHandler {

    List<Information> informationList = new ArrayList<>();
    Information info = null;

    boolean isPerson = false;
    boolean isRollNo = false;
    boolean isFullName = false;
    boolean isAge = false;
    boolean isAddress = false;
    boolean isEnjoy = false;
    boolean isprogrammingLanguage = false;

    @Override

    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("Person")) {
            info = new Information();
        } else if (qName.equalsIgnoreCase("rollNo")) {
            isRollNo = true;
        } 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("enjoy")) {
            isEnjoy = true;
        } else if (qName.equalsIgnoreCase("programmingLanguage")) {
            isprogrammingLanguage = true;
        }

    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("Person")) {
            informationList.add(info);
            info = null;
        } else if (qName.equalsIgnoreCase("rollNo")) {
            isRollNo = false;
        } 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("enjoy")) {
            isEnjoy = false;
        } else if (qName.equalsIgnoreCase("programmingLanguage")) {
            isprogrammingLanguage = false;
        }

    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String value = new String(ch, start, length);
        if (isRollNo) {
            info.setRollNo(value);
        } else if (isFullName) {
            info.setFullname(value);
        } else if (isAge) {
            info.setAge(Integer.parseInt(value));
        } else if (isAddress) {

            info.setAddress(value);
        } else if (isEnjoy) {
            info.getEnjoyList().add(value);
        } else if (isprogrammingLanguage) {
            info.getProgrammingLanguageList().add(value);

        }
    }

    void display() {
        for (Information informationList1 : informationList) {
            informationList1.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.test02;

import com.sun.org.apache.xml.internal.resolver.readers.SAXParserHandler;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;

/**
 *
 * @author Thien Phu
 */
public class Main {
    
    public static void main(String[] args) throws Exception {
        String filename = "D:\\baitapjava\\BaitapXML\\src\\xml\\test02\\infomation.xml";
        FileInputStream fis = new FileInputStream(filename);
        SAXParserFactory faxFactory = SAXParserFactory.newInstance();
        SAXParser saxParser = faxFactory.newSAXParser();
        
        InformationHander informationHander = new InformationHander();
        saxParser.parse(fis, informationHander);
        informationHander.display();
    }
}