<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的問題,出現(xiàn)了SAX。SAX
    ,事件驅(qū)動。當(dāng)解析器發(fā)現(xiàn)元素開始、元素結(jié)束、文本、文檔的開始或結(jié)束等時(shí),發(fā)送事件,程序員編寫響應(yīng)這些事件的代碼,保存數(shù)據(jù)。優(yōu)點(diǎn):不用事先調(diào)入整個文檔,占用資源少;SAX解析器代碼比DOM解析器代碼小,適于Applet,下載。缺點(diǎn):不是持久的;事件過后,若沒保存數(shù)據(jù),那么數(shù)據(jù)就丟了;無狀態(tài)性;從事件中只能得到文本,但不知該文本屬于哪個元素;使用場合:Applet;只需XML文檔的少量內(nèi)容,很少回頭訪問;機(jī)器內(nèi)存少;

    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("文檔打印結(jié)束了");  

    }  

    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("文檔打印結(jié)束了");
    }
    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)  編輯  收藏


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


    網(wǎng)站導(dǎo)航:
     

    導(dǎo)航

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

    統(tǒng)計(jì)

    常用鏈接

    留言簿(1)

    我參與的團(tuán)隊(duì)

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲一级片在线观看| 亚洲成在人天堂一区二区| 亚洲国产视频久久| 黄网站色在线视频免费观看| 亚洲黄色中文字幕| 亚洲精品国产免费| 亚洲电影在线免费观看| 中文字幕在线观看免费视频| 337p欧洲亚洲大胆艺术| 99久久精品日本一区二区免费 | 男人的好看免费观看在线视频| 亚洲精品偷拍无码不卡av| 69免费视频大片| 亚洲人成在线免费观看| 野花高清在线观看免费完整版中文| 亚洲AV无码久久久久网站蜜桃| 在线免费观看视频你懂的| 亚洲精品成a人在线观看夫| mm1313亚洲精品无码又大又粗| 国产精品1024在线永久免费| 久久香蕉国产线看观看亚洲片| 99久9在线|免费| 久久亚洲精品国产亚洲老地址| 亚洲JIZZJIZZ中国少妇中文| 在线观看免费视频一区| 亚洲视频在线视频| 成人无遮挡毛片免费看| 一级毛片高清免费播放| 久久亚洲免费视频| 成人性生交大片免费看午夜a| 国产精品亚洲五月天高清| 黑人精品videos亚洲人| 91短视频免费在线观看| 精品国产日韩亚洲一区在线| 亚洲伊人久久精品影院| 97人妻无码一区二区精品免费| 色哟哟国产精品免费观看| 亚洲激情在线观看| 成人免费视频国产| 久久久国产精品无码免费专区| 亚洲日韩国产AV无码无码精品|