您的位置:首页 > 其它

不积跬步无以至千里,不积小流无以成江海----SAX读取xml

2014-05-28 20:59 387 查看
sax 读取xml

import org.dom4j.Document;

import org.dom4j.Element;

import org.dom4j.io.OutputFormat;

import org.dom4j.io.SAXReader;

import org.dom4j.io.XMLWriter;

import org.xml.sax.Attributes;

import org.xml.sax.InputSource;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Iterator;

import java.util.List;

import javax.sql.rowset.spi.XmlWriter;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

class Test extends DefaultHandler{

    

    private StringBuffer buf;

    private String str;

    public Test(){

        super(); 

    }

    

//    public void setDocumentLocator(Locator locator){}

    @Override

    public void startDocument() throws SAXException{

        buf=new StringBuffer();

        System.out.println("*******开始解析文档*******");

    }

    @Override

    public void endDocument() throws SAXException{        

        System.out.println("*******文档解析结束*******");

    }

    @Override

    public void startPrefixMapping( String prefix, String uri ){

        System.out.println(" 前缀映射: " + prefix +" 开始!"+ " 它的URI是:" + uri);

    }

    @Override

    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 {}

    @Override

    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));

        }

    }

    @Override

    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);

    }

    @Override

    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();

            Test Test=new Test();

            sp.parse(new File("F:/text.xml"),Test);

        }catch(IOException e){

            e.printStackTrace(); 

        }catch(SAXException e){

            e.printStackTrace(); 

        }catch(Exception e){

            e.printStackTrace(); 

        }

    }

}

 

xml:

<?xml version="1.0" encoding="gb2312"?>

<person>

<name>王小明</name>

<college>信息学院</college> 

<telephone>6258113</telephone>

<notes>男,1955年生,博士,95年调入海南大学</notes>

</person>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  xml SAX