SAXParserFactory之求解
?SAX是Simple API for XML的簡(jiǎn)稱,在Android里面提供對(duì)XML文件的解析接口方法,如果給我們一個(gè)XML文件,要求把里面我們關(guān)心的數(shù)據(jù)解析出來,我們就可以使用SAX技術(shù),在具體使用中,會(huì)對(duì)XML文件的每一個(gè)字符逐一讀取并出發(fā)相應(yīng)事件,也就是說,SAX技術(shù)是事件驅(qū)動(dòng)的。比如startDocument,startElement,characters,endElement等等下面是一個(gè)案例。
實(shí)例源碼:
View Code1 public List<Person> getPersons(InputStream inStream) throws Throwable
2
3 {
4 SAXParserFactory factory = SAXParserFactory.newInstance();
5 SAXParser parser = factory.newSAXParser();
6 //自定義擴(kuò)展自DefaultHandler的子類,覆寫一些解析XML時(shí)我們需要的方法
7 PersonParser personParser = new PersonParser();
8 parser.parse(inStream, personParser);
9 //使用完傳進(jìn)來的輸入流后就把它關(guān)閉
10 inStream.close();
11 return personParser.getPersons();
12
13 }
SAXParserFactory 相關(guān)介紹:
定義了一個(gè)API工廠,使得應(yīng)用程序可以配置和獲得一個(gè)基于SAX(Simple API for XML
)的解析器,從而能夠解析XML文檔( 原文: Defines a factory API that enables applications to configure and obtain a SAX based parser to parse XML documents. )
它的構(gòu)造器是受保護(hù)的,因而只能用newInstance()方法獲得實(shí)例( Protected constructor to force use of?newInstance(). )
SAXParser相關(guān)介紹:
定義了一個(gè)繼承自XMLReader類的API,其構(gòu)造器也是受保護(hù)的,通過newSAXParser()?方法獲得實(shí)例,可以把各種數(shù)據(jù)源作為解析用的XML(這個(gè)方法就是public void parse (InputSource?is,?DefaultHandler?dh)),這些輸入數(shù)據(jù)源包括輸入流,文件,URL以及SAX輸入資源。(Defines the API that wraps an?XMLReader?implementation class , An instance of this class can be obtained from the?newSAXParser()?method. Once an instance of this class is obtained, XML can be parsed from a variety of input sources. These input sources are InputStreams, Files, URLs, and SAX InputSources. )
關(guān)于SAX更多使用信息,還會(huì)撰文。
轉(zhuǎn)載于:https://www.cnblogs.com/hpuCode/archive/2012/03/02/2377439.html
總結(jié)
以上是生活随笔為你收集整理的SAXParserFactory之求解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 好听的名字2个字的
- 下一篇: git获取指定release版本代码