<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    甜咖啡

    我的IT空間

    SAX生成和解析XML文檔

    2.SAX生成和解析XML文檔


    為解決DOM的問題,出現了SAX。SAX
    ,事件驅動。當解析器發現元素開始、元素結束、文本、文檔的開始或結束等時,發送事件,程序員編寫響應這些事件的代碼,保存數據。優點:不用事先調入整個文檔,占用資源少;SAX解析器代碼比DOM解析器代碼小,適于Applet,下載。缺點:不是持久的;事件過后,若沒保存數據,那么數據就丟了;無狀態性;從事件中只能得到文本,但不知該文本屬于哪個元素;使用場合:Applet;只需XML文檔的少量內容,很少回頭訪問;機器內存少;

    import java.io.FileInputStream;  
    import java.io.FileNotFoundException;  
    import java.io.IOException;  
    import java.io.InputStream;  

    import javax.xml.parsers.ParserConfigurationException;  
    import javax.xml.parsers.SAXParser;  
    import javax.xml.parsers.SAXParserFactory;  

    import org.xml.sax.Attributes;  
    import org.xml.sax.SAXException;  
    import org.xml.sax.helpers.DefaultHandler;  
    /** 
    *  
    * @author hongliang.dinghl 
    * SAX文檔解析 
    */ 
    public class SaxDemo implements XmlDocument {  

    public void createXml(String fileName) {  
    System.out.println("<<"+filename+">>");  
    }  

    public void parserXml(String fileName) {  
    SAXParserFactory saxfac = SAXParserFactory.newInstance();  

    try {  

    SAXParser saxparser = saxfac.newSAXParser();  

    InputStream is = new FileInputStream(fileName);  

    saxparser.parse(is, new MySAXHandler());  

    } catch (ParserConfigurationException e) {  

    e.printStackTrace();  

    } catch (SAXException e) {  

    e.printStackTrace();  

    } catch (FileNotFoundException e) {  

    e.printStackTrace();  

    } catch (IOException e) {  

    e.printStackTrace();  

    }  

    }  

    }  

    class MySAXHandler extends DefaultHandler {  

    boolean hasAttribute = false;  

    Attributes attributes = null;  

    public void startDocument() throws SAXException {  

    System.out.println("文檔開始打印了");  

    }  

    public void endDocument() throws SAXException {  

    System.out.println("文檔打印結束了");  

    }  

    public void startElement(String uri, String localName, String qName,  

    Attributes attributes) throws SAXException {  

    if (qName.equals("employees")) {  

    return;  

    }  

    if (qName.equals("employee")) {  

    System.out.println(qName);  

    }  

    if (attributes.getLength() > 0) {  

    this.attributes = attributes;  

    this.hasAttribute = true;  

    }  

    }  

    public void endElement(String uri, String localName, String qName)  

    throws SAXException {  

    if (hasAttribute && (attributes != null)) {  

    for (int i = 0; i < attributes.getLength(); i++) {  

    System.out.println(attributes.getQName(0)  
    + attributes.getValue(0));  

    }  

    }  

    }  

    public void characters(char[] ch, int start, int length)  

    throws SAXException {  

    System.out.println(new String(ch, start, length));  

    }  


    package com.alisoft.facepay.framework.bean;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
    /**
    *
    * @author hongliang.dinghl
    * SAX文檔解析
    */
    public class SaxDemo implements XmlDocument {
    public void createXml(String fileName) {
    System.out.println("<<"+filename+">>");
    }
    public void parserXml(String fileName) {
    SAXParserFactory saxfac = SAXParserFactory.newInstance();
    try {
    SAXParser saxparser = saxfac.newSAXParser();
    InputStream is = new FileInputStream(fileName);
    saxparser.parse(is, new MySAXHandler());
    } catch (ParserConfigurationException e) {
    e.printStackTrace();
    } catch (SAXException e) {
    e.printStackTrace();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    class MySAXHandler extends DefaultHandler {
    boolean hasAttribute = false;
    Attributes attributes = null;
    public void startDocument() throws SAXException {
    System.out.println("文檔開始打印了");
    }
    public void endDocument() throws SAXException {
    System.out.println("文檔打印結束了");
    }
    public void startElement(String uri, String localName, String qName,
    Attributes attributes) throws SAXException {
    if (qName.equals("employees")) {
    return;
    }
    if (qName.equals("employee")) {
    System.out.println(qName);
    }
    if (attributes.getLength() > 0) {
    this.attributes = attributes;
    this.hasAttribute = true;
    }
    }
    public void endElement(String uri, String localName, String qName)
    throws SAXException {
    if (hasAttribute && (attributes != null)) {
    for (int i = 0; i < attributes.getLength(); i++) {
    System.out.println(attributes.getQName(0)
    + attributes.getValue(0));
    }
    }
    }
    public void characters(char[] ch, int start, int length)
    throws SAXException {
    System.out.println(new String(ch, start, length));
    }
    }

    posted on 2011-07-19 16:32 甜咖啡 閱讀(357) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    導航

    <2011年7月>
    262728293012
    3456789
    10111213141516
    17181920212223
    24252627282930
    31123456

    統計

    常用鏈接

    留言簿(1)

    我參與的團隊

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产精品亚洲综合网站| 亚洲精品美女久久7777777 | 国产亚洲玖玖玖在线观看| 最近中文字幕免费2019| 亚洲丝袜美腿视频| 永久免费在线观看视频| 亚洲大片免费观看| 国产免费毛不卡片| 亚洲av中文无码乱人伦在线观看 | 欧美日韩国产免费一区二区三区| 色噜噜综合亚洲av中文无码| 免费A级毛片无码视频| 亚洲视频一区二区三区| 成年网站免费视频A在线双飞| 国产亚洲精品影视在线| 亚洲成av人在片观看| 热99RE久久精品这里都是精品免费 | 亚洲狠狠婷婷综合久久久久| 成人妇女免费播放久久久| 亚洲∧v久久久无码精品| **实干一级毛片aa免费| 亚洲日韩精品无码专区| 国产精品免费一级在线观看| 一个人看的www视频免费在线观看| 亚洲精品国产美女久久久| 久久99国产综合精品免费| 亚洲小说图区综合在线| 亚洲精品综合久久| 91精品国产免费入口| 亚洲av无码片vr一区二区三区| 久久精品国产精品亚洲人人| 在线成人爽a毛片免费软件| 亚洲日韩精品无码专区| 好看的亚洲黄色经典| 亚洲人成网站免费播放| 国产三级在线免费观看| 亚洲最大的视频网站| www国产亚洲精品久久久| 一级毛片免费毛片一级毛片免费| 亚洲av无码一区二区三区人妖| 亚洲熟女少妇一区二区|