新建工程,新建一个工具类PersonService,新建一个实体类Person,新建一个测试类Test
PerServiceTest类:
package com.mrzhu.pullparse; import java.io.OutputStream; import java.util.List; import org.xmlpull.v1.XmlSerializer; import android.util.Xml; public class PersonServiceTest { public void save(List<Person> persons, OutputStream out) throws Exception{ XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(out, "utf-8"); serializer.startDocument("utf-8", true); serializer.startTag(null, "persons"); for (Person person : persons) { serializer.startTag(null, "person"); serializer.attribute(null, "personid", person.getPersonid()); serializer.startTag(null, "name"); serializer.text(person.getName()); serializer.endTag(null, "name"); serializer.startTag(null, "tel"); serializer.text(person.getTel()); serializer.endTag(null, "tel"); serializer.startTag(null, "fax"); serializer.text(person.getFax()); serializer.endTag(null, "fax"); serializer.startTag(null, "email"); serializer.text(person.getEmail()); serializer.endTag(null, "email"); serializer.endTag(null, "person"); } serializer.endTag(null, "persons"); serializer.endDocument(); out.flush(); out.close(); } }
Person类:
package com.mrzhu.pullparse; public class Person { private String personid; private String name; private String address; private String tel; private String fax; private String email; public String getPersonid() { return personid; } public void setPersonid(String personid) { this.personid = personid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getFax() { return fax; } public void setFax(String fax) { this.fax = fax; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "Person [personid=" + personid + ", name=" + name + ", address=" + address + ", tel=" + tel + ", fax=" + fax + ", email=" + email + "]"; } public Person(String personid, String name, String address, String tel, String fax, String email) { super(); this.personid = personid; this.name = name; this.address = address; this.tel = tel; this.fax = fax; this.email = email; } }
Test类(继承AndroidTestCase):
1.在AndroidManifest.xml中的<application></application>标签中添加
<uses-library android:name="android.test.runner" />
2.<application></application>标签外添加
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.mrzhu.pullparse" >
</instrumentation>
android:targetPackage为测试类所在的包名
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mrzhu.pullparse" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".PullParseActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <uses-library android:name="android.test.runner" /> </application> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.mrzhu.pullparse" > </instrumentation> </manifest>
package com.mrzhu.pullparse; import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import android.test.AndroidTestCase; public class Test extends AndroidTestCase { public void testSave() throws Exception{ PersonServiceTest test = new PersonServiceTest(); //在工程下生成files文件,文件中有person.xml文件 //在File Explorer中展开data,在data中展开data,找到工程,展开工程可以看到files File file = new File(getContext().getFilesDir(), "person.xml"); FileOutputStream out = new FileOutputStream(file); List<Person> list = new ArrayList<Person>(); list.add(new Person("1", "Peter", "xxx.xxx", "123xxx", "123yyy", "123aaa")); list.add(new Person("2", "Lily", "aaa.aaa", "456xxx", "456yyy", "456aaa")); list.add(new Person("3", "Tom", "ccc.ccc", "789xxx", "789yyy", "789aaa")); test.save(list, out); } }