By GokiSoft.com| 19:40 25/03/2024|
XML

Tài liệu XML + ISA (Information System Analysis)



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

5

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

Nguyễn Văn Quang [T1907A]
Nguyễn Văn Quang

2020-04-16 14:26:45



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

    public Student() {
    }

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

    public String getFullname() {
        return fullname;
    }

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

    public String getAddress() {
        return address;
    }

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

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Student{" + "fullname=" + fullname + ", address=" + address + ", age=" + age + '}';
    }
    
    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 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 Quang
 */
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();
            StudentParser studentParser = new StudentParser();
            parser.parse(file, studentParser);
            List<Student>studentList = studentParser.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 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 Quang
 */
public class StudentParser extends DefaultHandler {

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

    public StudentParser() {
        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("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("fullname")) {
            isFullname = false;
        } else if (qName.equalsIgnoreCase("age")) {
            isAge = 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 (isAge) {
                String ageStr = new String(ch, start, length);
                currentStudent.setAge(Integer.parseInt(ageStr));
        } 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;
    }
    
    

}



lê văn phương [T1907A]
lê văn phương

2020-04-15 10:16:23



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

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 HOME
 */
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();
            StudentParser studentParser = new StudentParser();
            parser.parse(file, studentParser);
            List<Student>studentList = studentParser.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 Lesson1;

/**
 *
 * @author HOME
 */
public class Student {
    String fullname,address;
    int age;

    public Student() {
    }

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

    public String getFullname() {
        return fullname;
    }

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

    public String getAddress() {
        return address;
    }

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

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Student{" + "fullname=" + fullname + ", address=" + address + ", age=" + age + '}';
    }
    
    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 Lesson1;

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

/**
 *
 * @author HOME
 */
public class StudentParser extends DefaultHandler {

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

    public StudentParser() {
        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("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("fullname")) {
            isFullname = false;
        } else if (qName.equalsIgnoreCase("age")) {
            isAge = 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 (isAge) {
                String ageStr = new String(ch, start, length);
                currentStudent.setAge(Integer.parseInt(ageStr));
        } 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;
    }
    
    

}



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

<class_list>
	<class>
		<classname>T1907A</classname>
		<address>Hà Nội</address>
		<teacherName> Trần Văn Điệp</teacherName>
		<student_list>
			<student>
				<fullname>Lê Văn Phương</fullname>
				<age>20</age>
				<address>Thanh Hóa</address>
			</student>	
			<student>
				<fullname>Lê Văn B</fullname>
				<age>19</age>
				<address>Thanh Hóa</address>
			</student>	
		</student_list>	
	</class>	
</class_list>	



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

2020-04-15 09:56:57



/*
 * 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.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){
        try {
            File file = new File("studentList.xml");
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            
            StudentHandler studentHandler = new StudentHandler();
            try {
                parser.parse(file, studentHandler);
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            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);
        }
    }      
}



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

/**
 *
 * @author admin
 */
public class Student {
    String 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 admin
 */
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("rollno")){
            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("rollno")){
            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>Trần Mạnh Dũng</fullname>
               <age>18</age>
               <address>Phú Thọ</address>
               <email>tranmanhdung@gmail.com</email>
               <roll_no>R1</roll_no>
          </student>
          <student>
               <fullname>Nguyễn Hồng Huy</fullname>
               <age>18</age>
               <address>Phú Thọ</address>
               <email>nguyenhonghuy@gmail.com</email>
               <roll_no>R2</roll_no>
          </student>
    </studentList>
</root>



Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó