新建工程,新建一个工具类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;}@Overridepublic 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,找到工程,展开工程可以看到filesFile 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);}}



更多相关文章

  1. layout布局属性、标签属性总结大全
  2. Java工程中调用Android库出现“Stub!”错误
  3. android 反射解析xml文件成为java对象
  4. android 9.png 文件
  5. Android TXT文件读写
  6. android java复制和压缩文件或文件夹
  7. Android文件读写简单示例
  8. android获取图片文件头信息
  9. Android读写文件示例

随机推荐

  1. Android开发者指南(10) ―― Android(安
  2. Android开发常用代码片段(三)
  3. Android技术篇-了解Android的屏幕适配
  4. Android横竖屏切换
  5. Android(安卓)TextView多行文本滚动实现
  6. android 电容屏(二):驱动调试之基本概念篇
  7. Android(安卓)状态栏全透明策略
  8. android 电池(二):android关机充电流程、充
  9. 优秀的Android音频播放器
  10. Android(安卓)UI线程和非UI线程