首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

Android第六个效能:XmlPullParser解析XML文件

2013-03-29 
Android第六个功能:XmlPullParser解析XML文件?xml version1.0 encodingUTF-8?personsperson id

Android第六个功能:XmlPullParser解析XML文件
<?xml version="1.0" encoding="UTF-8"?><persons> <person id="1"> <name>dcj</name> <age>23</age> </person> <person id="2"> <name>lqq</name> <age>23</age> </person></persons>

?

package com.dcj.domain;public class Person {private Integer id;private String name;private Integer age;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";}}

?

package com.dcj.service;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import org.xmlpull.v1.XmlPullParser;import android.util.Xml;import com.dcj.domain.Person;public class PersonService {public static List<Person> getPersons(InputStream xml) throws Exception {List<Person> persons = null;Person person = null;XmlPullParser pullParser = Xml.newPullParser();pullParser.setInput(xml, "UTF-8");int event = pullParser.getEventType();while (event != XmlPullParser.END_DOCUMENT) {switch (event) {case XmlPullParser.START_DOCUMENT:persons = new ArrayList<Person>();break;case XmlPullParser.START_TAG:if ("person".equals(pullParser.getName())) {int id = new Integer(pullParser.getAttributeValue(0));person = new Person();person.setId(id);}if ("name".equals(pullParser.getName())) {String name = pullParser.nextText();person.setName(name);}if ("age".equals(pullParser.getName())) {int age = new Integer(pullParser.nextText());person.setAge(age);}break;case XmlPullParser.END_TAG:if("person".equals(pullParser.getName())) {persons.add(person);person = null;}break;}event = pullParser.next();}return persons;}}

?

package com.dcj.test;import java.io.InputStream;import java.util.List;import com.dcj.domain.Person;import com.dcj.service.PersonService;import android.test.AndroidTestCase;import android.util.Log;public class PersonServiceTest extends AndroidTestCase {private static final String TAG = "PersonServiceTest";public void testPersons() throws Exception {InputStream xml =this.getClass().getClassLoader().getResourceAsStream("person.xml");List<Person> persons = PersonService.getPersons(xml);for (Person person : persons) {Log.i(TAG, person.toString());}}}

?

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.dcj"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="8" />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <uses-library android:name="android.test.runner" />        <activity            android:name=".MainActivity"            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="com.dcj"         android:label="Test for my App"/>     </manifest>

?

?
Android第六个效能:XmlPullParser解析XML文件
?

热点排行