/* 自从XML真正形成以来(我认为是Org.W3C组织发布XML标准时开始),XML得到了很快的发展, 很多厂商都有推出了自己的XML解析器,如Apache的xalan,IBM的xerces,sun的JDOM等,不过这些都是在 基于JAXP(java API for XML processing)的,从JDK 1.4.0开始的后续j2sdk里都附加了JAXP,这给开发人员 带来了很大的方便,这使得我们在处理一般的XML功能上的问题时不再需要去用第三方的XML处理器了. 随着XML的迅速发展,SAX也从1.0到了现在的2.0了(还是能够和1.0兼容),结构上有了一些较大的变化.
DOM(document object model)每次读取XML节点时都要把它load到内存里来,在文档很大时,就显得很慢了,SAX(simple API for XML),是一个XML解析器的接口,它比DOM更低级一些,它是一种基于事件和回调模式的XML处理方式, 因此在解析速度上DOM是没法比的(当要解析的XML文档很大的时更是如此).那么在SAX中事件响应(event)是什么呢 ? 我个人认为这一点和Swing,AWT中的事件义有点相似的,都有是指在触发某些特定的行为时所做的处理,如:mouse 的click事件等到. 这里则是指碰到特定的XML节点的所做的处理,如文档开始(startDocument),文档结束(endDocument),元素开始(startElement)等很多,大家看一下SAX的API中的方法名字就知道有哪些事件了,基本上可以做到见文知义的.在只想分析XML内容(只读),要求高性能,灵活性 能够定位错误信息(SAX能够定位错误的行列位置)时,最好用SAX来做. 一般情况下SAX是按下面的原理去使用的: <1>设置事件处理器(SAX 1.0是使用一个通过继承HandlerBase类的实例来设置的,SAX 2.0则是继承DefaultHandler的,还有用XMLReader方式的,在原理上没有很大的区别) <2>载入要解析的内容 <3>在需要解析的事件方法里(具体参见SAX API文档)加入自己的控制逻辑. <4>重复<3>直到解析完为止. 在这里我自己写了一个描述电影海报信息的XML文件(file.xml),用SAX2.0写了一个很简单的XML内容阅读器来解析它, 和大家交流一下自己的心得.程序在我的机器上经过了测试的(OS: win2k Advanced Server(English version), Intel pentium CPU, 256M RAM) */
import org.w3c.dom.*; import org.xml.sax.*; import org.xml.sax.helpers.*; import javax.xml.parsers.*;
class MyXMLReader extends DefaultHandler { //Fields private int index; private Locator locator;
//Constructor public MyXMLReader(){ super(); //it must be done ! }
//nain method public static void main(String[] args){ try{ SAXParserFactory sf = SAXParserFactory.newInstance(); SAXParser sp = sf.newSAXParser(); MyXMLReader reader = new MyXMLReader(); sp.parse(new InputSource("film.xml"),reader); } catch(Exception e){ e.printStackTrace(); } }
//Response the startDocument event public void startDocument() { System.out.println("\n********************************* (: 元旦电影海报 :) ***********************************\n"); }
//Response the startElement event public void startElement(String uri, String localName, String qName, Attributes attrs){ if( qName.equalsIgnoreCase("film") ){ index ++; int attrCount = attrs.getLength(); for( int i = 0; i < attrCount; i ++ ){ String attrName = attrs.getQName(i); if( attrName.equalsIgnoreCase("name") ){ System.out.println("\t第" + index + "场,片名:<<" + attrs.getValue(i) + ">>"); } if( attrName.equalsIgnoreCase("price") ){ System.out.println("\t票价:" + attrs.getValue(i) ); } if( attrName.equalsIgnoreCase("station") ){ System.out.println("\t放映地点:" + attrs.getValue(i) ); } if( attrName.equalsIgnoreCase("time") ){ System.out.println("\t放映时间:" + attrs.getValue(i) ); } if( attrName.equalsIgnoreCase("describtion") ){ System.out.println("\t影片简介:" + attrs.getValue(i) ); } System.out.println(); } }
//Response the endDocument event public void endDocument(){ System.out.println("\t\t\t\t\t\t\t------ 共有" + index + "场电影要放映"); }
//Response the endElement event public void endElement(String uri, String localName, String qName){ //add your codes if neccessary ... }
//Print the fata error information public void fatalError(SAXParseException e){ System.out.println("\nFatal error information -->"); System.out.println("\t" + e.getMessage()); System.out.println("\tAt line " + locator.getLineNumber() + ",column " + locator.getColumnNumber()); } //Print the usual error information public void error(SAXParseException e){ System.out.println("\nUsual error information -->"); System.out.println("\t" + e.getMessage()); System.out.println("\tAt line " + locator.getLineNumber() + ",column " + locator.getColumnNumber()); } //Print the warning information public void warning(SAXParseException e){ System.out.println("\nWarning information -->"); System.out.println("\t" + e.getMessage()); System.out.println("\tAt line " + locator.getLineNumber() + ",column " + locator.getColumnNumber()); }
//Store the error locator object public void setDocumentLocator(Locator lct){ locator = lct; }
}//End class MyXMLReader
附: film.xml完全的内容:
<?xml version="1.0" encoding="GB2312"?> <!-- 2003年元月1号长沙市各大影院落放映列表 --> <common city="ChangSha China" date="01/01/2003"> <film name="英雄" price="30" station="田汉大剧场" time="19:00" describtion="国产最新大片,张艺谋导演,梁朝伟,张曼玉,李连杰等众多大明星主演"> </film> <film name="无间道" price="20" station="长沙市演艺中心" time="15:00" describtion="韩国大片"> </film> <film name="武士" price="20" station="湖南省电影院" time="17:00" describtion="韩国大片,有点像英雄"> </film> <film name="长排山之战" price="15" station="长沙市电影超市A1厅" time="19:00" describtion="反映对越自卫反击战时期中国军人的故事片"> </film> <film name="高山下的花环" price="15" station="长沙市电影超市A2厅" time="19:00" describtion="反映对越自卫反击战时期中国军人的故事片"> </film> <film name="这里的黎明静悄悄" price="15" station="长沙市电影超市A3厅" time="19:00" describtion="反映对越自卫反击战时期中国军人的故事片"> </film> <film name="子夜" price="15" station="长沙市电影超市B1厅" time="19:00" describtion="反映对越自卫反击战时期中国军人的故事片"> </film> </common>

|