11 November 2016

Tìm hiểu XML SAX cho người mới bắt đầu

1 - Mục đích của XML


  • Lưu trữ dữ liệu
  • Mô tả dữ liệu
2 - Ứng dụng XML
  • xml có thể là sử dụng cho java hoặc c#... đơn giản hóa việc trao đổi dữ liệu
  • vd: Game ta lưu trữ level của người chơi bằng XML
  • .....
3 - So sánh SAX và DOM
  • SAX mô hình sử lý theo sự kiện 
  • Chỉ đọc
  • It tốn bộ nhớ -> vì để file XML trên ổ cứng đọc trực tiếp trên ổ cứng

  • DOM sử lý theo đối tượng 
  • Đọc - ghi - sửa - xóa - tìm kiếm
  • Tốn bộ nhớ hơn  -> qua bước trung gian biến file XML thành cây đối tượng

sinhvien.xml
Java XML 2016
<?xml version="1.0" encoding="UTF-8"?>
<sinhvien>
    <masv>sv01</masv>
    <hoten>Sinh vien 1</hoten>
    <dtb>7</dtb>
</sinhvien>
DemoSAX.java
Java XML 2016
package demosax;

public class DemoSAX {

    public static void main(String[] args) {
        SinhVien sv = new SinhVien();
        sv.hienthi();
    }

}
SinhVien.java
Java XML 2016
package demosax;

import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class SinhVien {

    public void hienthi() {
        try {
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            sp.parse("src\\database\\sinhvien.xml", new SinhVienHandler());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    private class SinhVienHandler extends DefaultHandler {

        @Override
        public void startDocument() throws SAXException {
            System.out.println("Bat dau file xml");
        }

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            System.out.println("bat dau the");
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            System.out.println("Noi dung ben trong cua the");
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            System.out.println("Ket thuc the");
        }

        @Override
        public void endDocument() throws SAXException {
            System.out.println("Ket thuc file xml");
        }

    }
}
SinhVien.java
Java XML 2016
package demosax;

import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class SinhVien {

    public void hienthi() {
        try {
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            sp.parse("src\\database\\sinhvien.xml", new SinhVienHandler());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    private class SinhVienHandler extends DefaultHandler {

        @Override
        public void startDocument() throws SAXException {
            System.out.println("Bat dau file xml");
        }

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            System.out.println("bat dau the: " + qName);
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            System.out.println("Noi dung ben trong cua the");
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            System.out.println("Ket thuc the: " + qName);
        }

        @Override
        public void endDocument() throws SAXException {
            System.out.println("Ket thuc file xml");
        }

    }
}
SinhVien.java
Java XML 2016
package demosax;

import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class SinhVien {

    public void hienthi() {
        try {
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            sp.parse("src\\database\\sinhvien.xml", new SinhVienHandler());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    private class SinhVienHandler extends DefaultHandler {

        private String tagName = "";

        @Override
        public void startDocument() throws SAXException {
            System.out.println("Bat dau file xml");
        }

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            System.out.println("bat dau the: " + qName);
            this.tagName = qName;
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            if (this.tagName.equals("masv")) {
                System.out.println("Ma sinh vien: " + (new String(ch, start, length)));
                this.tagName = "";
            }
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            System.out.println("Ket thuc the: " + qName);
        }

        @Override
        public void endDocument() throws SAXException {
            System.out.println("Ket thuc file xml");
        }

    }
}
sinhvien.xml
Java XML 2016
<?xml version="1.0" encoding="UTF-8"?>
<sinhvien ngaysinh="1889-4-20">
    <masv>sv01</masv>
    <hoten>Sinh vien 1</hoten>
    <dtb min="0" max="10">7</dtb>
</sinhvien>
SinhVien.java
Java XML 2016
package demosax;

import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class SinhVien {

    public void hienthi() {
        try {
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            sp.parse("src\\database\\sinhvien.xml", new SinhVienHandler());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    private class SinhVienHandler extends DefaultHandler {

        private String tagName = "";

        @Override
        public void startDocument() throws SAXException {
            System.out.println("Bat dau file xml");
        }

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 
            this.tagName = qName;
            if(attributes.getLength() > 0 ){
                System.out.println("The: "+qName+" Co: "+ attributes.getLength() + " thuoc tinh");
                System.out.println("//===Danh sach cac thuoc tinh cua the: "+ qName);
                for (int i = 0; i < attributes.getLength(); i++) {
                    System.out.println(attributes.getQName(i) +":"+ attributes.getValue(i));
                }
            }
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            if (this.tagName.equals("masv")) {
                System.out.println("Ma sinh vien: " + (new String(ch, start, length)));
                this.tagName = "";
            }
            if (this.tagName.equals("hoten")) {
                System.out.println("Ho ten: " + (new String(ch, start, length)));
                this.tagName = "";
            }
            if (this.tagName.equals("dtb")) {
                System.out.println("Diem trung binh: " + (new String(ch, start, length)));
                this.tagName = "";
            }
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            System.out.println("Ket thuc the: " + qName);
        }

        @Override
        public void endDocument() throws SAXException {
            System.out.println("Ket thuc file xml");
        }

    }
}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang