/*
自從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>
?