甜咖啡

          我的IT空間

          SAX生成和解析XML文檔

          2.SAX生成和解析XML文檔


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

          }  

          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("文檔開(kāi)始打印了");
          }
          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 甜咖啡 閱讀(360) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


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

          導(dǎo)航

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

          統(tǒng)計(jì)

          常用鏈接

          留言簿(1)

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

          隨筆檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 万载县| 武威市| 寻乌县| 龙门县| 绥宁县| 靖州| 荥阳市| 静宁县| 库伦旗| 衡阳县| 文安县| 高淳县| 兴化市| 兰西县| 宁海县| 南和县| 阿勒泰市| 五大连池市| 渝中区| 晋中市| 喀什市| 安陆市| 永康市| 西畴县| 长乐市| 乌兰县| 阳新县| 财经| 兰考县| 凤山市| 江都市| 于田县| 兰西县| 汉川市| 镇康县| 伊吾县| 丰原市| 东丽区| 桐乡市| 泰宁县| 梨树县|