Android編程使用sax解析xml數(shù)據(jù)的方法詳解
本文實(shí)例講述了Android編程使用sax解析xml數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:
隨著技術(shù)的發(fā)展,現(xiàn)在的web已經(jīng)和以前不同了。web已經(jīng)逐漸像移動(dòng)的方向傾斜,作為程序員的確應(yīng)該拓展一下自己的知識(shí)層面。學(xué)習(xí)各方面的知識(shí),今天就接著前幾天的弄一下Android的xml解析,這次就使用sax的方式解析xml.下面就一步一步的來(lái)做吧。
1. 編寫(xiě)一個(gè)簡(jiǎn)單的xml
<?xml version="1.0" encoding="UTF-8"?> <persons> <person id="01"> <name>will</name> <age>21</age> </person> <person id="02"> <name>will2</name> <age>22</age> </person> </persons>
2. 編寫(xiě)pojo類(lèi)
package org.lxh.vo; public class Person { private String id; private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return "Person [id=" + id + ", name=" + name + ", age=" + age + "]"; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getId() { return id; } public void setId(String id) { this.id = id; } }
3. 寫(xiě)一個(gè)解析xml的類(lèi)
package org.lxh.impl; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.lxh.vo.Person; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import android.util.Log; public class Parse { public List<Person> findAll(InputStream in) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); //創(chuàng)建解析工廠 SAXParser parser = factory.newSAXParser(); ParsePerson chuli = new ParsePerson(); parser.parse(in, chuli); in.close(); //關(guān)閉輸入流 return chuli.getData(); } //開(kāi)始解析xml public class ParsePerson extends DefaultHandler { List<Person> all = null; Person person = null; String flag = null; public List<Person> getData() { return all; } public void startDocument() throws SAXException { all = new ArrayList<Person>(); //實(shí)例化集合 } public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if ("person".equals(localName)) { person = new Person(); person.setId(attributes.getValue(0)); //取得id的內(nèi)容 } flag = localName; //記錄上一個(gè)element } public void characters(char[] ch, int start, int length) throws SAXException { if (flag != null) { //這樣做取得的值就不會(huì)重復(fù) String data = new String(ch, start, length); if ("name".equals(flag)) { person.setName(data); } else if ("age".equals(flag)) { person.setAge(Integer.parseInt(data)); } } } public void endElement(String uri, String localName, String qName) throws SAXException { if ("person".equals(localName)) { all.add(person); person = null; } flag = null; } } }
4. 進(jìn)行單元測(cè)試
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.lxh.activity" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <uses-library android:name="android.test.runner"/> <activity android:name=".SaxParseXmlActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="org.lxh.activity" android:label="TestforMyApp"/> <uses-sdk android:minSdkVersion="8" /> </manifest>
package org.lxh.activity; import java.io.InputStream; import java.util.Iterator; import java.util.List; import org.lxh.impl.Parse; import org.lxh.vo.Person; import android.test.AndroidTestCase; import android.util.Log; public class Test extends AndroidTestCase{ public static final String tag="Test"; public void testShuchu() throws Throwable{ //Log.i(tag, "123"); Parse p=new Parse(); InputStream in=getClass().getClassLoader().getResourceAsStream("persons.xml"); List<Person> all=p.findAll(in); Log.i(tag, String.valueOf(all.size())); Iterator<Person> it=all.iterator(); while(it.hasNext()){ Person person=it.next(); Log.i(tag, person.toString()); } } }
最后來(lái)看一下運(yùn)行效果圖,這里最好弄個(gè)filter,控制臺(tái)就沒(méi)那么亂了。
點(diǎn)擊那個(gè)綠色的加號(hào)就OK了
PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android操作XML數(shù)據(jù)技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android 快速實(shí)現(xiàn)狀態(tài)欄透明樣式的示例代碼
下面小編就為大家分享一篇Android 快速實(shí)現(xiàn)狀態(tài)欄透明樣式的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01Android中實(shí)現(xiàn)HashMap排序的方法
這篇文章主要介紹了Android中實(shí)現(xiàn)HashMap排序的方法,很經(jīng)典的一種排序算法,需要的朋友可以參考下2014-08-08Android 7.0系統(tǒng)webview 顯示https頁(yè)面空白處理方法
今天小編就為大家分享一篇Android 7.0系統(tǒng)webview 顯示https頁(yè)面空白處理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07Android編程實(shí)現(xiàn)webview將網(wǎng)頁(yè)打包成apk的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)webview將網(wǎng)頁(yè)打包成apk的方法,以打包HTML5為例分析了webview打包apk的相關(guān)方法、屬性與事件操作技巧,需要的朋友可以參考下2017-08-08Android WebView 內(nèi)處理302重定向不跳轉(zhuǎn)的解決
這篇文章主要介紹了Android WebView 內(nèi)處理302重定向不跳轉(zhuǎn)的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03解決android studio 3.0 加載項(xiàng)目過(guò)慢問(wèn)題--maven倉(cāng)庫(kù)選擇
這篇文章主要介紹了android studio 3.0 加載項(xiàng)目過(guò)慢問(wèn)題解決方案---maven倉(cāng)庫(kù)選擇,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11RecycleView實(shí)現(xiàn)各種尺寸圖片展示
這篇文章主要為大家詳細(xì)介紹了RecycleView實(shí)現(xiàn)各種尺寸圖片展示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05