InputSource inputSource = new InputSource(xmlURI); reader.parse(inputSource);
這樣一個xml文檔解析過程就完成了。因為SAX是采用時間處理機制來解析XML 文檔的,在解析過程中會觸發一些事件,也就是執行特定的方法,你可以實現 這些方法,就可以通過解析xml來做一些事情了 2)處理 SAX2.0定義的核心處理接口一共有 org.xml.sax.ContentHandler org.xml.sax.ErrorHandler org.xml.sax.DTDHandler org.xml.sax.EntityResolver 這些接口是通過 org.xml.sax.XMLReader的setContentHandler(),setEroorHandler(), setDTDHandler(),setEntityHandler()注冊到解析器,這里面最重要的是 org.xml.sax.ContentHandler接口,它具體如下 public interface ContentHandler{ public void setDocumentLocator(Locator locator); public void startDocument() throws SAXException; public void endDocument() throws SAXException; public void startPRefixMapping(String prefix,String uri) throws SAXException; public void endPrefixMapping(String prifix) throws SAXException; public void startElement(String namespaceURI,String localName, String qName,Attributes atts) throws SAXException; public void endElement(String namespaceURI,String localName, String qName) throws SAXException; public void characters(char ch[],int start,int length) throws SAXException; public void ignorableWhitespace(char ch[],int start,int length) throws SAXException; public void processingInstrUCtion(String target,String data) throws SAXException; public void skippedEntity(String name) throws SAXException; } 通過setContentHandler()將你實現的ContentHandler注冊給XMLReader之后, 在解析過程中,系統根據各種條件執行接口中的方法,下面簡單說明一下 1)文檔定位器 private Locator locator;
public void setDocumentLocator(Locator locator){ this.locator = locator; }
通常情況下,你只要如此實現就可以了,這個主要是得到當前解析的位置, 通過得到的locator,你可以使用它的getLineNumber(),getColumnName()等 方法,可以得到文檔當前的位置,但要注重的是,這個locator不能保存,只 針對當前的解析有效 2)文檔的開頭和結尾 public void startDocument() throws SAXException{ //解析過程中僅位于setDocumentLocator()方法后調用 } public void endDocument() throws SAXException{ //解析過程中最后調用 }
大多數情況下你可以不用理他們,只要寫個空方法就可以了 3)名字空間的開始和結束 public void startPrefixMapping(String prefix,String uri) throws SAXException{ } public void endPrefixMapping(String prifix) throws SAXException{ } 4)元素的開始和結束 public void startElement(String namespaceURI,String localName, String qName,Attributes atts) throws SAXException{ } public void endElement(String namespaceURI,String localName, String qName) throws SAXException{ } 5)元素的數據 public void characters(char ch[],int start,int length) throws SAXException{ String s = new String(ch,start,length); } 這個是得到當前的元素的文本數據 6)可以忽略的空白 public void ignorableWhitespace(char ch[],int start,int length) throws SAXException{ } 7)實體 public void skippedEntity(String name) throws SAXException{ } 8)指令處理 public void processingInstruction(String target,String data) throws SAXException{ } 3)例子:這個是從Java & XML 中復制過來的, /* * Created on 2004-11-30 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package javaxml2;
/** * @author yuangfang * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */
public SAXTreeViewer(){ super("SAX Tree Viewer"); setSize(600,450); }
public void init(String xmlURI) throws IOException,SAXException{ DefaultMutableTreeNode base = new DefaultMutableTreeNode("XML Document:" + xmlURI); defaultTreeModel = new DefaultTreeModel(base); jTree = new JTree(defaultTreeModel);
InputSource inputSource = new InputSource(xmlURI); reader.parse(inputSource); } catch(SAXNotRecognizedException e){ System.out.println("The parse class " + vendorParserClass + " does not recognize the feature URI " + featureURI); System.exit(0); } catch(SAXNotSupportedException e){ System.out.println("The parser class " + vendorParserClass + " does not support the feature URI " + featureURI); } }