<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)

    我參與的團隊

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产婷婷成人久久Av免费高清 | 亚洲精品无码永久在线观看 | 国产一区视频在线免费观看| 日本一线a视频免费观看| 免费又黄又硬又爽大片| 亚洲人成影院在线| 精品亚洲AV无码一区二区三区 | 毛片高清视频在线看免费观看| 国产高清免费观看| 亚洲va无码va在线va天堂| 亚洲高清视频在线播放| 99亚洲乱人伦aⅴ精品| 香蕉免费一级视频在线观看| 在线人成精品免费视频| 国产小视频在线观看免费| 风间由美在线亚洲一区| 免费一级毛片不卡不收费| 高潮毛片无遮挡高清免费| 99xxoo视频在线永久免费观看| 国产无遮挡裸体免费视频 | 日本一区二区三区免费高清在线| 中文字幕免费观看视频| 成年人免费视频观看| 亚洲小视频在线观看| 69式互添免费视频| 爱情岛论坛网亚洲品质自拍| 亚洲高清中文字幕免费| 本免费AV无码专区一区| 亚洲AV一宅男色影视| 亚洲大片免费观看| 亚洲gv白嫩小受在线观看| 最近免费最新高清中文字幕韩国| 亚洲mv国产精品mv日本mv| 日本高清免费不卡在线| 成人妇女免费播放久久久| 亚洲精品成人网站在线播放 | 色猫咪免费人成网站在线观看 | 久久久久久精品成人免费图片 | 伊人久久五月丁香综合中文亚洲 | 精品久久久久久亚洲中文字幕| 免费一级国产生活片|