中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

SAX之內(nèi)容處理Java類

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用

為了讓應(yīng)用程序有效地處理XML數(shù)據(jù),你必須向SAX解析器注冊處理程序。處理程序也稱Handler接口,是由SAX定義的一組回調(diào)方法組成的,這些方法使你可以在相關(guān)的事件發(fā)生時對其進(jìn)行編程。

在SAX2.0中定義了四大核心接口:org.xml.sax.ContentHandler,org.xml.sax.ErrorHandler,org.xml.sax.DTDHandler以及org.xml.sax.EntityResolver。

其于SAX的XML應(yīng)用程序必須實現(xiàn)一個或多個Handler接口,并為Handler接口中的回調(diào)方法編程(如果你不想添加代碼,也可以不寫,這樣就可以忽略這種類型的事件)。然后使用XMLReader中的setContentHandler(),setErrorHandler(),setDTDHandler()和setEntityResolver()方法進(jìn)行注冊即可。在解析過程中,reader會在合適的處理類中調(diào)用這些回調(diào)方法。

我們將從實現(xiàn)ContentHandler接口開始講解,ContentHandler,顧名思義,表示的是與XML文檔內(nèi)容處理有關(guān)的事件,如元素,屬性,字符數(shù)據(jù)等。示例:

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接口實現(xiàn),這里沒有實現(xiàn)什么內(nèi)容,稍后實現(xiàn)
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
            
        }
}


文檔定位器

第一個需要實現(xiàn)的回調(diào)方法是setDocumentLocator(),它用于設(shè)置在其他SAX事件中需要使用的org.xml.sax.Locator對象。當(dāng)某個回調(diào)事件被觸發(fā)時,實現(xiàn)了SAX解析器處理接口的類通常需要在XML文件中找到相應(yīng)的位置。Locator類有幾個很有用的方法,例如getLineNumber()和getColumnNumber(),它們可以返回調(diào)用時正在解析的XML文檔的位置。例:

class MyContentHandler implements ContentHandler {
    private Locator locator;
    @Override
    public void setDocumentLocator(Locator locator) {
        this.locator = locator;
    }
    //...........其它回調(diào)方法...............
}


警告:Locator實例只能在ContentHandler實現(xiàn)的作用域范圍內(nèi)使用,在解析過程外,使用Locator對象,其結(jié)果是難以預(yù)料的(也是無用的)。

文檔解析的開始和結(jié)束

任何處理過程都有開始和結(jié)束。這兩個重要的事件都只會發(fā)生一次:前者在所有其他解析事件發(fā)生之前發(fā)生,而后者則是在所有解析事件之后發(fā)生。SAX提供了回調(diào)方法:startDocument()和endDocument()來表示這些事件。

startDocument()方法在所有其他解析事件的回調(diào)方法之前被調(diào)用,這樣就保證了解析有一個明確的起始點(diǎn)。endDocument()在所有處理類中總是最后被調(diào)用的方法,即使在發(fā)生錯誤從而導(dǎo)致解析被終止的情況下也是如此。注意:若有不可恢復(fù)的錯誤發(fā)生,則ErrorHandler類的回調(diào)方法就會被調(diào)用,最后再調(diào)用endDocument()方法結(jié)束解析過程。示例代碼:

@Override
public void startDocument() throws SAXException {
    // TODO Auto-generated method stub
}
@Override
public void endDocument() throws SAXException {
    // TODO Auto-generated method stub
 }

示例代碼中,無需對這些方法進(jìn)行任何處理,但因為實現(xiàn)了ContentHandler接口,因而仍給出方法的實現(xiàn)。

處理指令

來自:http://my.oschina.net/fhd/blog/367402

標(biāo)簽: 代碼

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:簡單的DOM的API封裝

下一篇:Java通過zip壓縮文件