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

声明:

该blog是为了收集资料,认识朋友,学习、提高技术,所以本blog的内容除非声明,否则一律为转载!!

感谢那些公开自己技术成果的高人们!!!

支持开源,尊重他人的劳动!!

常用链接

留言簿(3)

随笔分类(148)

随笔档案(143)

收藏夹(2)

其他

学习(技术)

观察思考(非技术)

搜索

  •  

最新评论

阅读排行榜

评论排行榜

虽然有些方法没用,但还是保留了下来!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(
"*******开始解析文档*******");
    }

    
    
public void endDocument() throws SAXException{        
        System.out.println(
"*******文档解析结束*******");
    }

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

    
    
public void endPrefixMapping( String prefix ){
        System.out.println(
" 前缀映射: "+prefix+" 结束!");
    }

    
//    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(
"*******开始解析元素*******");    
        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+"元素解析结束"+localName+"********"+fullName);
    }

    
    
public void characters( char[] chars, int start, int length )throws SAXException{
        
//将元素内容累加到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文件,我读的时候有错误,用了自己的,希望其他人比我幸运!
代码:
<?xml version="1.0" encoding="GB2312"?>
<row>
<person>
<name>王小明</name>
<college>信息学院</college> 
<telephone>6258113</telephone>
<notes>男,1955年生,博士,95年调入海南大学</notes>
</person>
</row> 
posted on 2007-11-22 10:44 lk 阅读(9132) 评论(2)  编辑  收藏 所属分类: j2sexml

FeedBack:
# re: java sax 解析 xml 2008-12-14 11:12 咕嘎
确实出现了一些问题,例子还是不错的  回复  更多评论
  
# re: java sax 解析 xml 2008-12-24 01:43 路过
</row> 这里后面多了一个空格也会报错的  回复  更多评论
  
# re: java sax 解析 xml 2009-08-21 19:19 王大帅
<?xml version="1.0" encoding="GB2312"?>
<row>
<person>
<name>王小明</name>
<college>信息学院</college>
<telephone>6258113</telephone>
<notes>男,1955年生,博士,95年调入海南大学</notes>
</person>
</row> 
这个XML是不对的,不符合XML语法,改成这样就可以了
<?xml version="1.0" encoding="GB2312"?>

<person>
<name>王小明</name>
<college>信息学院</college>
<telephone>6258113</telephone>
<notes>男,1955年生,博士,95年调入海南大学</notes>
</person>
  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: