數(shù)據(jù)加載中……
          JAVA XML 解析之四 -SAX
          2008年4月21日  Edited By DingDangXiaoMa
          SAX 是采用事件驅(qū)動(dòng)的方式與Dom最大的不同是,它不用讀取完所有的XML文件就可以進(jìn)行處理。采用流式的處理方式(呵,呵,我也不知道是什么意思)
          example:
          注冊(cè)內(nèi)容管理器:MyContentHandler.java
          public class MyContentHandler implements ContentHandler {
              
          // DTD中定義的元素
              private static final String ELEMENT_NAME = "name";
              
          private static final String ELEMENT_SEX = "sex";
              
          private static final String ELEMENT_LESSON = "lesson";
              
          private static final String ELEMENT_LESSON_NAME = "lessonName";
              
          private static final String ELEMENT_LESSON_SCORE = "lessonScore";
              
          private static final String ELEMENT_STUDENT = "student";
              
          private static final String ELEMENT_LINE = "breakLine";

              
          private String currentData = ""// 當(dāng)前元素的數(shù)據(jù)
              private String lessonName = "";
              
          private String lessonScore = "";

              
          public MyContentHandler() {
              }

              
          /**
               * 當(dāng)其他某一個(gè)調(diào)用事件發(fā)生時(shí),先調(diào)用此方法來在文檔中定位。
               * 
               * 
          @param locator
               
          */
              
          public void setDocumentLocator(Locator locator) {

              }

              
          /**
               * 在解析整個(gè)文檔開始時(shí)調(diào)用
               * 
               * 
          @throws SAXException
               
          */
              
          public void startDocument() throws SAXException {
                  System.out.println(
          "**** Student information start ****");
              }

              
          /**
               * 在解析整個(gè)文檔結(jié)束時(shí)調(diào)用
               * 
               * 
          @throws SAXException
               
          */
              
          public void endDocument() throws SAXException {
                  System.out.println(
          "**** Student information end ****");
              }

              
          /**
               * 在解析名字空間開始時(shí)調(diào)用
               * 
               * 
          @param prefix
               * 
          @param uri
               * 
          @throws SAXException
               
          */
              
          public void startPrefixMapping(String prefix, String uri)
                      
          throws SAXException {

              }

              
          /**
               * 在解析名字空間結(jié)束時(shí)調(diào)用
               * 
               * 
          @param prefix
               * 
          @throws SAXException
               
          */
              
          public void endPrefixMapping(String prefix) throws SAXException {

              }

              
          /**
               * 在解析元素開始時(shí)調(diào)用
               * 
               * 
          @param namespaceURI
               * 
          @param localName
               * 
          @param qName
               * 
          @param atts
               * 
          @throws SAXException
               
          */
              
          public void startElement(String namespaceURI, String localName,
                      String qName, Attributes atts) 
          throws SAXException {

              }

              
          /**
               * 在解析元素結(jié)束時(shí)調(diào)用
               * 
               * 
          @param namespaceURI
               * 
          @param localName
               *            本地名,如student
               * 
          @param qName
               *            原始名,如LIT:student
               * 
          @throws SAXException
               
          */
              
          public void endElement(String namespaceURI, String localName, String qName)
                      
          throws SAXException {
                  
          if (localName.equals(ELEMENT_NAME)) {
                      System.out.println(localName 
          + ":" + currentData);
                  }

                  
          if (localName.equals(ELEMENT_SEX)) {
                      System.out.println(localName 
          + ":" + currentData);
                  }

                  
          if (localName.equals(ELEMENT_LESSON_NAME)) {
                      
          this.lessonName = currentData;
                  }

                  
          if (localName.equals(ELEMENT_LESSON_SCORE)) {
                      
          this.lessonScore = currentData;
                  }

                  
          if (localName.equals(ELEMENT_LESSON)) {
                      System.out.println(lessonName 
          + ":" + lessonScore);
                  }

                  
          if (localName.equals(ELEMENT_LINE)) {
                      System.out.println(
          "------------------------------------");
                  }
              }

              
          /**
               * 取得元素?cái)?shù)據(jù)
               * 
               * 
          @param ch
               * 
          @param start
               * 
          @param length
               * 
          @throws SAXException
               
          */
              
          public void characters(char[] ch, int start, int length)
                      
          throws SAXException {
                  currentData 
          = new String(ch, start, length).trim();
              }

              
          /**
               * 取得元素?cái)?shù)據(jù)中的空白
               * 
               * 
          @param ch
               * 
          @param start
               * 
          @param length
               * 
          @throws SAXException
               
          */
              
          public void ignorableWhitespace(char[] ch, int start, int length)
                      
          throws SAXException {

              }

              
          /**
               * 在解析到處理指令時(shí),調(diào)用此方法。 這些處理指令不包括XML的版權(quán)指令,它由解析器本身識(shí)別。
               * 
               * 
          @param target
               * 
          @param data
               * 
          @throws SAXException
               
          */
              
          public void processingInstruction(String target, String data)
                      
          throws SAXException {

              }

              
          /**
               * 當(dāng)未驗(yàn)證解析器忽略實(shí)體時(shí)調(diào)用此方法
               * 
               * 
          @param name
               * 
          @throws SAXException
               
          */
              
          public void skippedEntity(String name) throws SAXException {

              }
          錯(cuò)誤管理器:MyErrorHandler.java
          public class MyErrorHandler implements ErrorHandler {

              
          public MyErrorHandler() {
              }

              
          /**
               * XML的警告信息
               * 
               * 
          @param exception
               * 
          @throws SAXException
               
          */
              
          public void warning(SAXParseException exception) throws SAXException {
                  System.out.println(
          "!!!WARNING!!!");
                  System.out.println(exception.getLineNumber() 
          + ":("
                          
          + exception.getSystemId() + ")" + exception.getMessage());
              }

              
          /**
               * 不符合XML規(guī)范時(shí)調(diào)用此方法
               * 
               * 
          @param exception
               * 
          @throws SAXException
               
          */
              
          public void error(SAXParseException exception) throws SAXException {
                  System.out.println(
          "!!!ERROR!!!");
                  System.out.println(exception.getLineNumber() 
          + ":("
                          
          + exception.getSystemId() + ")" + exception.getMessage());
              }

              
          /**
               * 非良構(gòu)的文檔時(shí)調(diào)用此方法
               * 
               * 
          @param exception
               * 
          @throws SAXException
               
          */
              
          public void fatalError(SAXParseException exception) throws SAXException {
                  System.out.println(
          "!!!FATAL!!!");
                  System.out.println(exception.getLineNumber() 
          + ":("
                          
          + exception.getSystemId() + ")" + exception.getMessage());
              }
          }
          DTD 管理器: MyDTDHandler.java
          public class MyDTDHandler implements DTDHandler {

              
          public MyDTDHandler() {
              }

              
          /**
               * 當(dāng)實(shí)體聲明為不必解析的實(shí)體時(shí)調(diào)用此方法,比如NDATA類型。
               * 
               * 
          @param name
               * 
          @param publicId
               * 
          @param systemId
               * 
          @throws SAXException
               
          */
              
          public void notationDecl(String name, String publicId, String systemId)
                      
          throws SAXException {
                  System.out.println(
          "**notationDecl**");
                  System.out.println(
          "name:" + name);
                  System.out.println(
          "publicId" + publicId);
                  System.out.println(
          "systemId:" + systemId);
              }

              
          /**
               * 當(dāng)處理符號(hào)聲明時(shí)調(diào)用此方法,比如NOTATION。
               * 
               * 
          @param name
               * 
          @param publicId
               * 
          @param systemId
               * 
          @param notationName
               * 
          @throws SAXException
               
          */
              
          public void unparsedEntityDecl(String name, String publicId,
                      String systemId, String notationName) 
          throws SAXException {
                  System.out.println(
          "**unparsedEntityDecl**");
                  System.out.println(
          "name:" + name);
                  System.out.println(
          "publicId" + publicId);
                  System.out.println(
          "systemId:" + systemId);
                  System.out.println(
          "notationName:" + notationName);
              }
          }
          SAX方式處理:MySAXParser.java
          import java.io.IOException;
          import org.xml.sax.SAXException;
          import org.xml.sax.XMLReader;
          import org.xml.sax.helpers.XMLReaderFactory;
          public class MySAXParser {
              
          public MySAXParser() {
              }
              
          public static void main(String[] args) {
                  MySAXParser mySAXParser 
          = new MySAXParser();
                  mySAXParser.parserXMLFile(
          "http://localhost/struts2.0/xml/SutInfo.xml");
              }
              
          /**
               * 解析文檔XML文檔的URI
               * 
          @param fileURI
               
          */
              
          private void parserXMLFile(String fileURI) {
                  
          try {
                      
          // 通過指定解析器的名稱來動(dòng)態(tài)加載解析器
                      XMLReader parser = XMLReaderFactory
                              .createXMLReader(
          "org.apache.xerces.parsers.SAXParser");
                      
          // 處理內(nèi)容前要注冊(cè)內(nèi)容管理器
                      parser.setContentHandler(new MyContentHandler());
                      
          // 處理錯(cuò)誤前要注冊(cè)錯(cuò)誤管理器
                      parser.setErrorHandler(new MyErrorHandler());
                      
          // 處理DTD前要注冊(cè)DTD管理器
                      parser.setDTDHandler(new MyDTDHandler());
                      
          // 打開解析器的驗(yàn)證
                      parser.setFeature("http://xml.org/sax/features/validation"true);
                      
          // 開始解析文檔
                      parser.parse(fileURI);
                  } 
          catch (IOException ioe) {
                      System.out.println(ioe.getMessage());
                  } 
          catch (SAXException saxe) {
                      System.out.println(saxe.getMessage());
                  }
              }
          }
          說明:
          org.xml.sax.*,包含于jdk 中。
          這個(gè)例子是 javaresearch 上的例子,我也沒有弄明白是什么意思,呵。呵。誰若是看到了些貼,請(qǐng)回復(fù),說一說您的看法。

          posted on 2008-04-21 17:34 叮當(dāng)小馬 閱讀(193) 評(píng)論(0)  編輯  收藏 所屬分類: XML

          主站蜘蛛池模板: 来凤县| 桦南县| 双桥区| 武城县| 榆林市| 铜鼓县| 巴青县| 兴城市| 合川市| 磴口县| 临泉县| 互助| 临沭县| 福贡县| 彩票| 黄浦区| 从化市| 平阴县| 莱州市| 邳州市| 图片| 景泰县| 名山县| 工布江达县| 故城县| 玛纳斯县| 佛教| 公主岭市| 赤峰市| 彭水| 瓮安县| 静安区| 醴陵市| 天镇县| 佛山市| 志丹县| 台安县| 太康县| 河北区| 七台河市| 新邵县|