數據加載中……
          JAVA XML 解析之四 -SAX
          2008年4月21日  Edited By DingDangXiaoMa
          SAX 是采用事件驅動的方式與Dom最大的不同是,它不用讀取完所有的XML文件就可以進行處理。采用流式的處理方式(呵,呵,我也不知道是什么意思)
          example:
          注冊內容管理器: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 = ""// 當前元素的數據
              private String lessonName = "";
              
          private String lessonScore = "";

              
          public MyContentHandler() {
              }

              
          /**
               * 當其他某一個調用事件發生時,先調用此方法來在文檔中定位。
               * 
               * 
          @param locator
               
          */
              
          public void setDocumentLocator(Locator locator) {

              }

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

              
          /**
               * 在解析整個文檔結束時調用
               * 
               * 
          @throws SAXException
               
          */
              
          public void endDocument() throws SAXException {
                  System.out.println(
          "**** Student information end ****");
              }

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

              }

              
          /**
               * 在解析名字空間結束時調用
               * 
               * 
          @param prefix
               * 
          @throws SAXException
               
          */
              
          public void endPrefixMapping(String prefix) throws SAXException {

              }

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

              }

              
          /**
               * 在解析元素結束時調用
               * 
               * 
          @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(
          "------------------------------------");
                  }
              }

              
          /**
               * 取得元素數據
               * 
               * 
          @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();
              }

              
          /**
               * 取得元素數據中的空白
               * 
               * 
          @param ch
               * 
          @param start
               * 
          @param length
               * 
          @throws SAXException
               
          */
              
          public void ignorableWhitespace(char[] ch, int start, int length)
                      
          throws SAXException {

              }

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

              }

              
          /**
               * 當未驗證解析器忽略實體時調用此方法
               * 
               * 
          @param name
               * 
          @throws SAXException
               
          */
              
          public void skippedEntity(String name) throws SAXException {

              }
          錯誤管理器: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規范時調用此方法
               * 
               * 
          @param exception
               * 
          @throws SAXException
               
          */
              
          public void error(SAXParseException exception) throws SAXException {
                  System.out.println(
          "!!!ERROR!!!");
                  System.out.println(exception.getLineNumber() 
          + ":("
                          
          + exception.getSystemId() + ")" + exception.getMessage());
              }

              
          /**
               * 非良構的文檔時調用此方法
               * 
               * 
          @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() {
              }

              
          /**
               * 當實體聲明為不必解析的實體時調用此方法,比如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);
              }

              
          /**
               * 當處理符號聲明時調用此方法,比如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 {
                      
          // 通過指定解析器的名稱來動態加載解析器
                      XMLReader parser = XMLReaderFactory
                              .createXMLReader(
          "org.apache.xerces.parsers.SAXParser");
                      
          // 處理內容前要注冊內容管理器
                      parser.setContentHandler(new MyContentHandler());
                      
          // 處理錯誤前要注冊錯誤管理器
                      parser.setErrorHandler(new MyErrorHandler());
                      
          // 處理DTD前要注冊DTD管理器
                      parser.setDTDHandler(new MyDTDHandler());
                      
          // 打開解析器的驗證
                      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 中。
          這個例子是 javaresearch 上的例子,我也沒有弄明白是什么意思,呵。呵。誰若是看到了些貼,請回復,說一說您的看法。

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

          主站蜘蛛池模板: 徐汇区| 凌源市| 扶沟县| 黔西县| 太湖县| 海丰县| 宝应县| 古浪县| 通化市| 昌邑市| 娄底市| 玉龙| 焉耆| 开封市| 长寿区| 且末县| 保亭| 临泽县| 盐亭县| 徐闻县| 洛宁县| 池州市| 保亭| 保靖县| 广德县| 盐池县| 德保县| 勃利县| 双流县| 扬中市| 库车县| 大庆市| 田林县| 清原| 新余市| 洱源县| 东乌| 遵化市| 芜湖县| 扶沟县| 浦北县|