甜咖啡

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


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


          網站導航:
           

          導航

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

          統計

          常用鏈接

          留言簿(1)

          我參與的團隊

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 连山| 泰来县| 含山县| 咸阳市| 黑龙江省| 永修县| 搜索| 利川市| 呼图壁县| 扎兰屯市| 揭阳市| 大庆市| 台中县| 内黄县| 西充县| 报价| 兴仁县| 赤水市| 湘潭县| 广宁县| 宁德市| 嘉义县| 达州市| 公主岭市| 达日县| 黄山市| 香格里拉县| 涞源县| 那曲县| 高清| 遵义市| 土默特左旗| 延庆县| 衡南县| 南溪县| 吐鲁番市| 柳河县| 临沂市| 东莞市| 内乡县| 舞阳县|