SAX之內容處理Java類
為了讓應用程序有效地處理XML數據,你必須向SAX解析器注冊處理程序。處理程序也稱Handler接口,是由SAX定義的一組回調方法組成的,這些方法使你可以在相關的事件發生時對其進行編程。
在SAX2.0中定義了四大核心接口:org.xml.sax.ContentHandler,org.xml.sax.ErrorHandler,org.xml.sax.DTDHandler以及org.xml.sax.EntityResolver。
其于SAX的XML應用程序必須實現一個或多個Handler接口,并為Handler接口中的回調方法編程(如果你不想添加代碼,也可以不寫,這樣就可以忽略這種類型的事件)。然后使用XMLReader中的setContentHandler(),setErrorHandler(),setDTDHandler()和setEntityResolver()方法進行注冊即可。在解析過程中,reader會在合適的處理類中調用這些回調方法。
我們將從實現ContentHandler接口開始講解,ContentHandler,顧名思義,表示的是與XML文檔內容處理有關的事件,如元素,屬性,字符數據等。示例:
package xml; import java.io.File; import java.io.FileInputStream; import org.xml.sax.Attributes; import org.xml.sax.ContentHandler; import org.xml.sax.InputSource; import org.xml.sax.Locator; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.XMLReaderFactory; public class ParseXML1 { public static void main(String[] args) { try { XMLReader reader = XMLReaderFactory.createXMLReader(); reader.setContentHandler(new MyContentHandler()); //注冊 reader.parse(new InputSource(new FileInputStream( new File("/home/fuhd/apk/gw/com.application.zomato.apk/AndroidManifest.xml")))); } catch (SAXException e1) { e1.printStackTrace(); } catch(Exception e2){ e2.printStackTrace(); } } } //Handler接口實現,這里沒有實現什么內容,稍后實現 class MyContentHandler implements ContentHandler { @Override public void setDocumentLocator(Locator locator) { // TODO Auto-generated method stub } @Override public void startDocument() throws SAXException { // TODO Auto-generated method stub } @Override public void endDocument() throws SAXException { // TODO Auto-generated method stub } @Override public void startPrefixMapping(String prefix, String uri) throws SAXException { // TODO Auto-generated method stub } @Override public void endPrefixMapping(String prefix) throws SAXException { // TODO Auto-generated method stub } @Override public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { // TODO Auto-generated method stub } @Override public void endElement(String uri, String localName, String qName) throws SAXException { // TODO Auto-generated method stub } @Override public void characters(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub } @Override public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub } @Override public void processingInstruction(String target, String data) throws SAXException { // TODO Auto-generated method stub } @Override public void skippedEntity(String name) throws SAXException { // TODO Auto-generated method stub } }
文檔定位器
第一個需要實現的回調方法是setDocumentLocator(),它用于設置在其他SAX事件中需要使用的org.xml.sax.Locator對象。當某個回調事件被觸發時,實現了SAX解析器處理接口的類通常需要在XML文件中找到相應的位置。Locator類有幾個很有用的方法,例如getLineNumber()和getColumnNumber(),它們可以返回調用時正在解析的XML文檔的位置。例:
class MyContentHandler implements ContentHandler { private Locator locator; @Override public void setDocumentLocator(Locator locator) { this.locator = locator; } //...........其它回調方法............... }
警告:Locator實例只能在ContentHandler實現的作用域范圍內使用,在解析過程外,使用Locator對象,其結果是難以預料的(也是無用的)。
文檔解析的開始和結束
任何處理過程都有開始和結束。這兩個重要的事件都只會發生一次:前者在所有其他解析事件發生之前發生,而后者則是在所有解析事件之后發生。SAX提供了回調方法:startDocument()和endDocument()來表示這些事件。
startDocument()方法在所有其他解析事件的回調方法之前被調用,這樣就保證了解析有一個明確的起始點。endDocument()在所有處理類中總是最后被調用的方法,即使在發生錯誤從而導致解析被終止的情況下也是如此。注意:若有不可恢復的錯誤發生,則ErrorHandler類的回調方法就會被調用,最后再調用endDocument()方法結束解析過程。示例代碼:
@Override public void startDocument() throws SAXException { // TODO Auto-generated method stub } @Override public void endDocument() throws SAXException { // TODO Auto-generated method stub }
示例代碼中,無需對這些方法進行任何處理,但因為實現了ContentHandler接口,因而仍給出方法的實現。
處理指令
來自:http://my.oschina.net/fhd/blog/367402