學習sax解析xml心得

          可以使用SAXParser的parse方法進行解析,也可以使用XmlReader的parse方法進行解析,但是最好是使用XmlReader,因為XmlReader是接口。
          基本的初始化方法如下:
          File f=new File("d:"+File.separator+"e.xml");
                  InputSource ip=new InputSource(new FileInputStream(f));
                  try {
                      SAXParser  s=SAXParserFactory.newInstance().newSAXParser();
                      XMLReader xmlReader=s.getXMLReader();
                      xmlReader.setContentHandler(new MyHandler());
                      xmlReader.parse(ip);
                      
                  } catch (ParserConfigurationException e) {
                      e.printStackTrace();
                  } catch (SAXException e) {
                      e.printStackTrace();
                  }

          其中MyHandler類繼承了DefaultHandler,可以overwrite其中的方法,使其滿足需求。
          例子:

          public void startElement(String uri, String localName, String name,
                          Attributes attributes) throws SAXException {

                      if (logger.isInfoEnabled()) {
                          logger
                                  .info("startElement(String, String, String, Attributes) - uri="
                                          + uri
                                          + ", localName="
                                          + localName
                                          + ", name="
                                          + name );
                      }

                      for (int i = 0; i < attributes.getLength(); i++) {
                          if (logger.isInfoEnabled()) {
                              logger.info("endElement(String, String, String) - uri=" + uri
                                      + ", localName=" + localName + ", qName=" + attributes.getQName(i)+ ", attributes=" + attributes.getValue(i));
                          }
                      }
                      

                      super.startElement(uri, localName, name, attributes);
                  }
          //對text node的處理
          public void characters(char[] ch, int start, int length)
                          throws SAXException {
                      if (logger.isInfoEnabled()) {
                          logger.info("characters(char[], int, int) - ch="+ new String(ch,start,length));//這里這樣子寫才能得到text node 真正的值。范圍: characters() 事件不僅包括不僅一個字符串。它還包括起始和長度信息。實際上,ch 字符數組包括整個文檔。應用程序一定不能嘗試讀取饋送給 characters() 事件的范圍之外的字符。
                      }

                      super.characters(ch, start, length);
                  }


          posted on 2007-11-15 17:27 劉錚 閱讀(312) 評論(0)  編輯  收藏 所屬分類: XML

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          留言簿(1)

          文章分類(141)

          文章檔案(147)

          搜索

          最新評論

          主站蜘蛛池模板: 锡林郭勒盟| 寿宁县| 金阳县| 乌拉特后旗| 甘德县| 广南县| 新余市| 临洮县| 渭南市| 武安市| 扶余县| 富民县| 兴海县| 石阡县| 乌恰县| 宜良县| 班戈县| 宜都市| 绥棱县| 滦平县| 高阳县| 宁南县| 滕州市| 台南市| 日照市| 肃南| 西平县| 托克托县| 绵阳市| 安平县| 福州市| 阿瓦提县| 小金县| 苏州市| 茂名市| 丹巴县| 平罗县| 潼关县| 靖边县| 西安市| 德江县|