SAX解析xml学习笔记[1]

Java类:TestXmlSAXHandler (监听器)

package  hello.xml;

import  org.xml.sax.helpers.DefaultHandler;
import  org.xml.sax.helpers.XMLReaderFactory;
import  org.xml.sax.SAXException;
import  org.xml.sax.XMLReader;
import  org.xml.sax.InputSource;
import  org.xml.sax.Attributes;
import  java.io.InputStream;
import  java.io.File;
import  java.io.FileInputStream;
import  java.io.FileReader;
import  java.util.HashMap;



/**
 * 测试SAX
 * 
@author l
 *
 
*/

public   class  TestXmlSAXHandler  extends  DefaultHandler {

    
private HashMap<String,String> tags;
    
    
/**
     * 开始读xml文档的方法
     
*/

    
public void startDocument() throws SAXException{
        System.out.println(
".startDocument..Ready to parser XML document..");
        tags
=new HashMap<String,String>();
    }

    
    
/**
     * 在解释到一个开始元素时会调用此方法.但是当元素有重复时可以自己写算法来区分
     * 这些重复的元素.qName是什么?<name:page ll=""></name:page>这样写就会抛出SAXException错误
     * 通常情况下qName等于localName
     
*/

    
public void startElement(String uri,String localName,
            String qName,Attributes attributes) 
throws SAXException{
        
        System.out.println(
".startElement..parsering XML document.the localName is."+localName);
        System.out.println(
".startElement..parsering XML document.the qName is."+qName);
        System.out.println(
".startElement..attribute localName is."+attributes.getLocalName(0));
        System.out.println(
".startElement..attribute qName is."+attributes.getQName(1));
        System.out.println(
".startElement..attribute value is."+attributes.getValue(attributes.getQName(1)));
        
    }

    
    
/**
     * 在遇到结束标签时调用此方法
     
*/

    
public void endElement(String uri, String localName, String qName)throws SAXException{
        System.out.println(
".endElement..parsering XML document.the localName is."+localName);
    }

    
    
/**
     * 读取标签里的值,ch用来存放某行的xml的字符数据,包括标签,初始大小是2048,每解释到新的字符会把它添加到char[]里。
     * 注意,这个char字符会自己管理存储的字符,并不是每一行就会刷新一次char,start,length是由xml的元素数据确定的,
     * 暂时找不到规律,以后看源代码.
     
*/

    
public void characters(char[] ch,int start,int length) throws SAXException{
//        System.out.println(".characters..get the .ch."+ch.length);
//        System.out.println(".characters..get the .start."+start);
//        System.out.println(".characters..get the .length."+length);
//        System.out.println(".characters..get the .hashCode."+ch.hashCode());
//        System.out.println(".characters..ch,0,39.-->"+new String(ch,0,start));
        System.out.println(".characters..ch,start,length.-->"+new String(ch,start,length));
    }

    
    
/**
     * 结束解析xml文档
     
*/

    
public void endDocument()throws SAXException{
        System.out.println(
".endDocument..finish to parser XML document..");
    }

    
    
public static void main(String[]args){
        TestXmlSAXHandler handler
=new TestXmlSAXHandler();
        File file
=new File("hello/xml/MyXml.xml");
        FileReader input
=null;
        InputSource is
=null;
//        InputStream input=new InputStream();
        
        
        XMLReader xmlReader
=null;
        
try{
            input
=new FileReader(file);
            is
=new InputSource(input);
          xmlReader
=XMLReaderFactory.createXMLReader();
          xmlReader.setContentHandler(handler);
          xmlReader.parse(is);
          
        }
catch(Exception e){
            e.printStackTrace();
          }

        }

    
}

 xml文件:MyXml.xml

 

<? xml version="1.0" encoding="UTF-8" ?>
< POEM >
< AUTHOR  go ="hello"  to ="my" > sai </ AUTHOR >
< TITLE  oo ="11" > java </ TITLE >
< LINE > tangle </ LINE >
</ POEM >

输出:

.startDocument..Ready to parser XML document..
.startElement..parsering XML document.the localName is.POEM
.startElement..parsering XML document.the qName is.POEM
.startElement..attribute localName is.null
.startElement..attribute qName is.null
.startElement..attribute value is.null
.characters..ch,start,length.-->

.startElement..parsering XML document.the localName is.AUTHOR
.startElement..parsering XML document.the qName is.AUTHOR
.startElement..attribute localName is.go
.startElement..attribute qName is.to
.startElement..attribute value is.my
.characters..ch,start,length.-->sai
.endElement..parsering XML document.the localName is.AUTHOR
.characters..ch,start,length.-->

.startElement..parsering XML document.the localName is.TITLE
.startElement..parsering XML document.the qName is.TITLE
.startElement..attribute localName is.oo
.startElement..attribute qName is.null
.startElement..attribute value is.null
.characters..ch,start,length.-->java
.endElement..parsering XML document.the localName is.TITLE
.characters..ch,start,length.-->

.startElement..parsering XML document.the localName is.LINE
.startElement..parsering XML document.the qName is.LINE
.startElement..attribute localName is.null
.startElement..attribute qName is.null
.startElement..attribute value is.null
.characters..ch,start,length.-->tangle
.endElement..parsering XML document.the localName is.LINE
.characters..ch,start,length.-->

.endElement..parsering XML document.the localName is.POEM
.endDocument..finish to parser XML document..

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值