Android編程使用sax解析xml數(shù)據(jù)的方法詳解
本文實例講述了Android編程使用sax解析xml數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:
隨著技術(shù)的發(fā)展,現(xiàn)在的web已經(jīng)和以前不同了。web已經(jīng)逐漸像移動的方向傾斜,作為程序員的確應(yīng)該拓展一下自己的知識層面。學(xué)習(xí)各方面的知識,今天就接著前幾天的弄一下Android的xml解析,這次就使用sax的方式解析xml.下面就一步一步的來做吧。
1. 編寫一個簡單的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. 編寫pojo類
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. 寫一個解析xml的類
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(); } //開始解析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>(); //實例化集合 } 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; //記錄上一個element } public void characters(char[] ch, int start, int length) throws SAXException { if (flag != null) { //這樣做取得的值就不會重復(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. 進行單元測試
<?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()); } } }
最后來看一下運行效果圖,這里最好弄個filter,控制臺就沒那么亂了。
點擊那個綠色的加號就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)容感興趣的讀者可查看本站專題:《Android操作XML數(shù)據(jù)技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android開發(fā)入門與進階教程》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Android 快速實現(xiàn)狀態(tài)欄透明樣式的示例代碼
下面小編就為大家分享一篇Android 快速實現(xiàn)狀態(tài)欄透明樣式的示例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01Android 7.0系統(tǒng)webview 顯示https頁面空白處理方法
今天小編就為大家分享一篇Android 7.0系統(tǒng)webview 顯示https頁面空白處理方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07Android編程實現(xiàn)webview將網(wǎng)頁打包成apk的方法
這篇文章主要介紹了Android編程實現(xiàn)webview將網(wǎng)頁打包成apk的方法,以打包HTML5為例分析了webview打包apk的相關(guān)方法、屬性與事件操作技巧,需要的朋友可以參考下2017-08-08Android WebView 內(nèi)處理302重定向不跳轉(zhuǎn)的解決
這篇文章主要介紹了Android WebView 內(nèi)處理302重定向不跳轉(zhuǎn)的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03解決android studio 3.0 加載項目過慢問題--maven倉庫選擇
這篇文章主要介紹了android studio 3.0 加載項目過慢問題解決方案---maven倉庫選擇,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-11-11