隨筆 - 154  文章 - 60  trackbacks - 0
          <2007年11月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          聲明:

          該blog是為了收集資料,認(rèn)識(shí)朋友,學(xué)習(xí)、提高技術(shù),所以本blog的內(nèi)容除非聲明,否則一律為轉(zhuǎn)載?。?br />
          感謝那些公開(kāi)自己技術(shù)成果的高人們!?。?/p> 支持開(kāi)源,尊重他人的勞動(dòng)?。?

          常用鏈接

          留言簿(3)

          隨筆分類(148)

          隨筆檔案(143)

          收藏夾(2)

          其他

          學(xué)習(xí)(技術(shù))

          觀察思考(非技術(shù))

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          雖然有些方法沒(méi)用,但還是保留了下來(lái)!String  str是我加的

          代碼:
          import org.xml.sax.Attributes;
          import org.xml.sax.SAXException;
          import org.xml.sax.Locator;
          import org.xml.sax.ContentHandler;
          import org.xml.sax.InputSource;
          import org.xml.sax.helpers.DefaultHandler;
          import java.io.IOException;
          import javax.xml.parsers.SAXParser;
          import javax.xml.parsers.SAXParserFactory;


          class TestSAX extends DefaultHandler{
              
              
          private StringBuffer buf;
              
          private String str;
              
          public TestSAX(){
                  
          super(); 
              }

              
          //    public void setDocumentLocator(Locator locator){}
              
              
          public void startDocument() throws SAXException{
                  buf
          =new StringBuffer();
                  System.out.println(
          "*******開(kāi)始解析文檔*******");
              }

              
              
          public void endDocument() throws SAXException{        
                  System.out.println(
          "*******文檔解析結(jié)束*******");
              }

              
              
          public void startPrefixMapping( String prefix, String uri ){
                  System.out.println(
          " 前綴映射: " + prefix +" 開(kāi)始!"+ " 它的URI是:" + uri);
              }

              
              
          public void endPrefixMapping( String prefix ){
                  System.out.println(
          " 前綴映射: "+prefix+" 結(jié)束!");
              }

              
          //    public void processingInstruction( String target, String instruction )throws SAXException{}
              
          //    public void ignorableWhitespace( char[] chars, int start, int length ) throws SAXException {}
              
          //    public void skippedEntity( String name ) throws SAXException {}
              
              
          public void startElement(String namespaceURI,String localName,String qName,Attributes atts){
                  System.out.println(
          "*******開(kāi)始解析元素*******");    
                  System.out.println(
          "元素名"+qName);        
                  
          for(int i=0;i<atts.getLength();i++){
                      System.out.println(
          "元素名"+atts.getLocalName(i)+"屬性值"+atts.getValue(i));
                  }

              }

              
              
          public void endElement(String namespaceURI,String localName,String fullName )throws SAXException{
          //        buf.trimToSize();
                  str = buf.toString(); 
                  System.out.println(
          "buf = "+buf+" || length = "+buf.length());
                  System.out.println(
          "str = "+str.trim()+" || length = "+str.trim().length());
                  buf.delete(
          0,buf.length());
                  System.out.println(
          "******"+namespaceURI+"元素解析結(jié)束"+localName+"********"+fullName);
              }

              
              
          public void characters( char[] chars, int start, int length )throws SAXException{
                  
          //將元素內(nèi)容累加到StringBuffer中 
                  buf.append(chars,start,length);
              }

              
              
          public static void main(String args[]){
                  
          try{
                      SAXParserFactory sf 
          = SAXParserFactory.newInstance();
                      SAXParser sp 
          = sf.newSAXParser();
                      TestSAX testsax
          =new TestSAX();
                      sp.parse(
          new InputSource("test1.xml"),testsax);
                  }
          catch(IOException e){
                      e.printStackTrace(); 
                  }
          catch(SAXException e){
                      e.printStackTrace(); 
                  }
          catch(Exception e){
                      e.printStackTrace(); 
                  }

              }

          }

          xml文件,我讀的時(shí)候有錯(cuò)誤,用了自己的,希望其他人比我幸運(yùn)!
          代碼:
          <?xml version="1.0" encoding="GB2312"?>
          <row>
          <person>
          <name>王小明</name>
          <college>信息學(xué)院</college> 
          <telephone>6258113</telephone>
          <notes>男,1955年生,博士,95年調(diào)入海南大學(xué)</notes>
          </person>
          </row> 
          posted on 2007-11-22 10:44 lk 閱讀(9156) 評(píng)論(2)  編輯  收藏 所屬分類: j2se 、xml

          FeedBack:
          # re: java sax 解析 xml 2008-12-14 11:12 咕嘎
          確實(shí)出現(xiàn)了一些問(wèn)題,例子還是不錯(cuò)的  回復(fù)  更多評(píng)論
            
          # re: java sax 解析 xml 2008-12-24 01:43 路過(guò)
          </row> 這里后面多了一個(gè)空格也會(huì)報(bào)錯(cuò)的  回復(fù)  更多評(píng)論
            
          # re: java sax 解析 xml 2009-08-21 19:19 王大帥
          <?xml version="1.0" encoding="GB2312"?>
          <row>
          <person>
          <name>王小明</name>
          <college>信息學(xué)院</college>
          <telephone>6258113</telephone>
          <notes>男,1955年生,博士,95年調(diào)入海南大學(xué)</notes>
          </person>
          </row> 
          這個(gè)XML是不對(duì)的,不符合XML語(yǔ)法,改成這樣就可以了
          <?xml version="1.0" encoding="GB2312"?>

          <person>
          <name>王小明</name>
          <college>信息學(xué)院</college>
          <telephone>6258113</telephone>
          <notes>男,1955年生,博士,95年調(diào)入海南大學(xué)</notes>
          </person>
            回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 鲜城| 泰州市| 平乐县| 齐河县| 叶城县| 盖州市| 吉安县| 开鲁县| 陇川县| 康乐县| 石渠县| 遂平县| 北宁市| 汉源县| 台州市| 丰原市| 岑溪市| 秀山| 柘城县| 青海省| 同江市| 曲阜市| 扎鲁特旗| 县级市| 山阴县| 桦川县| 方城县| 华坪县| 读书| 辛集市| 山东省| 庆元县| 万年县| 宣城市| 北安市| 乐亭县| 南部县| 龙门县| 孝感市| 辽阳市| 民勤县|