java SAX 解析XML

虽然有些方法没用,但还是保留了下来!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 version="1.0" encoding="GB2312" ?>
< person >
< name > 王小明 </ name >
< college > 信息学院 </ college >  
< telephone > 6258113 </ telephone >
< notes > 男,1955年生,博士,95年调入海南大学 </ notes >
</ person >
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值