SAXtext

package org.accp.xml.dom;

import java.io.IOException;
import java.util.Arrays;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.ext.DefaultHandler2;
import org.xml.sax.helpers.DefaultHandler;

public class SAXTest extends DefaultHandler {
 
 private StringBuffer buf;
    private String str;
    public SAXTest(){
        super();
    }
   
//    public void setDocumentLocator(Locator locator){}
   
    @Override
    public void startDocument() throws SAXException {
     // TODO Auto-generated method stub
        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中
       System.out.print("\t当前元素文本:");
     System.out.println(Arrays.copyOfRange(chars, start,start+length));
     
     buf.append(chars,start,length);
    }
   
    public static void main(String args[]){
        try{
            SAXParserFactory sf = SAXParserFactory.newInstance();
            SAXParser sp = sf.newSAXParser();
            SAXTest testsax=new SAXTest();
            sp.parse(new InputSource("pets2.xml"),testsax);
        }catch(IOException e){
            e.printStackTrace();
        }catch(SAXException e){
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

 

}

阅读更多
个人分类: XML
上一篇pet2.xml
下一篇pet.template,NewFile.xml,pets.dtd(这是三个文件)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭