<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 甜咖啡 閱讀(363) 評論(0)  編輯  收藏


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


    網站導航:
     

    導航

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

    統計

    常用鏈接

    留言簿(1)

    我參與的團隊

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲人成网站18禁止久久影院| 最近中文字幕国语免费完整| 国产一级淫片免费播放电影| 亚洲国产电影在线观看| 国产午夜亚洲精品国产成人小说| 免费无码又爽又刺激聊天APP| 丁香花在线视频观看免费| 国产精品亚洲五月天高清| 在线观看亚洲免费| 黄色免费网址大全| 91在线亚洲综合在线| 伊人久久大香线蕉亚洲五月天| 久久综合九色综合97免费下载| 色多多A级毛片免费看| 亚洲精品无码专区久久| 亚洲日本国产乱码va在线观看| 在线免费一区二区| 日本h在线精品免费观看| 毛片亚洲AV无码精品国产午夜| 亚洲日韩中文字幕在线播放| 亚洲国产精品一区二区九九| 麻豆精品国产免费观看| 国产免费区在线观看十分钟| 国产成人不卡亚洲精品91 | 久久久精品免费国产四虎| 国产99精品一区二区三区免费 | 亚洲国产中文v高清在线观看| 少妇人妻偷人精品免费视频| 日韩中文字幕免费| 我想看一级毛片免费的| 免费一本色道久久一区| 成年人在线免费观看| 一二三四影视在线看片免费| 一区在线免费观看| 一级人做人爰a全过程免费视频 | 精品国产一区二区三区免费看| 色窝窝免费一区二区三区| 老司机在线免费视频| aaa毛片免费观看| 不卡视频免费在线观看| 亚洲精品久久久久无码AV片软件|