(一)關(guān)于 解析XML格式數(shù)據(jù)

解析之前,我們可以先搭建個(gè)本地的Web服務(wù)器,使用Apache服務(wù)器(下載地址:百度即有提供下載),安裝完成后在htdocs目錄下新建一個(gè)get_data.xml文件,這時(shí)在瀏覽器訪問(wèn)127.0.0.1/get_data.xml這個(gè)網(wǎng)址,就能出現(xiàn)你新建文件的內(nèi)容。
解析XML數(shù)據(jù)常用的有兩種解析方式:Pull解析和SAX解析。(一)Pull解析XmlPullParserFactory xmlPullParserFactory=XmlPullParserFactory.newInstance(); XmlPullParser xmlPullParser=xmlPullParserFactory.newPullParser();
首先獲取到XMLPullParserFactory的實(shí)例(newInstance()),并借助這個(gè)實(shí)例得到XMLPullParser的對(duì)象(newPullParser()),接著通過(guò)xmlPullParser.setInput(new StringReader(xmlData))開始解析服務(wù)器返回的數(shù)據(jù),解析過(guò)程:通過(guò)getEventType()得到當(dāng)前解析事件(就是某個(gè)結(jié)點(diǎn)??!),int eventType=xmlPullParser.getEventType(); String id="";String name="";String version=""; while(eventType!=XmlPullParser.END_DOCUMENT){ String nodename=xmlPullParser.getName(); switch(eventType){//開始解析某個(gè)結(jié)點(diǎn) case XmlPullParser.START_TAG: if("id".equals(nodename)){ id=xmlPullParser.nextText();//nextText()獲取某個(gè)結(jié)點(diǎn)的具體內(nèi)容 }else if("name".equals(nodename)){ name=xmlPullParser.nextText(); }else if("version".equals(nodename)){ version=xmlPullParser.nextText();} break; //完成某個(gè)結(jié)點(diǎn)的解析 case XmlPullParser.END_TAG: if("app".equals(nodename)){ Log.d("MainActivity"," id is "+id); Log.d("MainActivity"," name is "+name); Log.d("MainActivity"," version is "+version);} break; default: break;} eventType=xmlPullParser.next();//調(diào)用next()獲取下一個(gè)結(jié)點(diǎn)!?。?}}catch(Exception e){e.printStackTrace();}
(二)SAX解析:新建一個(gè)ContentHandler類繼承DefaultHandler類,并重寫部分方法:1、startDocument()(官方文檔Receive notification of the beginning of the document):每處理一個(gè)XML文檔都會(huì)響應(yīng)一次。所以這個(gè)方法里可以寫需要初始化的代碼。2、startElement(String uri,String localName,String qName,Attributes attributes)(官方文檔Receive notification of the beginning of the element):這是處理每個(gè)結(jié)點(diǎn)所觸發(fā)的方法,通過(guò)這個(gè)方法你可以直接當(dāng)前處理的節(jié)點(diǎn)的名稱以及屬性。3、characters(char[] ch,int start,int length)(官方文檔Receive notification of character data inside an element.):會(huì)在獲取結(jié)點(diǎn)中的內(nèi)容的時(shí)候調(diào)用,但該方法不會(huì)告訴你文本所屬的標(biāo)簽。4、endElement(String uri,String localName,String qName)(官方文檔Receive notification of the end of an element.): 遇到一個(gè)結(jié)點(diǎn)的結(jié)束標(biāo)簽時(shí),將會(huì)出發(fā)這個(gè)方法,并且會(huì)傳遞結(jié)束標(biāo)簽的名稱。5、endDocument()(官方文檔Receive notification of the end of the document.);完成整個(gè)XML解析時(shí)調(diào)用。而在MainActivity中先建了一個(gè)SAXParserFactory的對(duì)象(newInstance()),再獲取到XMLReader的對(duì)象(SAXParserFactory.newSAXParser().getXMLReader()),接著將我們寫好的ContentHandler的實(shí)例設(shè)置到XMLReader中(xmlReader.setContentHandlet(ContentHandler handler)(Allow an application to register a content event handler),再調(diào)用xmlReader.parse(InputSource input)解析XML文件

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 1 XML解析No29 【 XML:可拓展標(biāo)記語(yǔ)言,語(yǔ)言和HTML類似,也是一種標(biāo)記語(yǔ)言。 特點(diǎn):標(biāo)記是自定義...
    征程_Journey閱讀 1,684評(píng)論 0 9
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,890評(píng)論 0 23
  • 1. XML解析的兩種方式 服務(wù)器返回的XML數(shù)據(jù)如下 1.1使用Pull方式解析XML數(shù)據(jù) 使用Pull解析,首...
    figure_ai閱讀 418評(píng)論 0 2
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,923評(píng)論 18 139
  • Pull解析方式比如解析以下一段xml字符串: 示例代碼:
    Chebyshev閱讀 5,039評(píng)論 0 3