新建Android项目
编写AndroidManifest.xml,使本Android项目具有单元测试功能和写外设的权限。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima28.xmldemo" android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" />
<!—用于单元测试—> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.itheima28.xmldemo"> </instrumentation>
<!—写外设的权限à <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <!—单元测试所需à <uses-library android:name="android.test.runner"/>
<activity android:name="com.itheima28.xmldemo.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>
</manifest>
|
3 Person对象
package com.itheima28.xmldemo;
public class Person {
private int id; private String name; private int age;
@Override public String toString() { return "Person [id=" + id + ", name=" + name + ", age=" + age + "]"; } public Person() { super(); } public Person(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }
} |
4 写xml文件和读xml文件的代码
package com.itheima28.xmldemo.test;
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlSerializer;
import android.os.Environment; import android.test.AndroidTestCase; import android.util.Log; import android.util.Xml;
import com.itheima28.xmldemo.Person;
public class TestCase extends AndroidTestCase {
/** * 测试写xml文件 */ public void testWriteXml() { writeXmlToLocal(); }
/** * 测试读xml文件 */ public void testReadXml() { List<Person> personList = parseXmlFromLocal(); for (Person person : personList) { Log.i("TestCase",person.toString()); } }
/** * 写xml文件到本地 */ private void writeXmlToLocal() { List<Person> personList = getPersonList();
//获得序列化对象 XmlSerializer serializer = Xml.newSerializer();
try { //测试获得到的位置是:/storage/emulated/0 File path = new File(Environment.getExternalStorageDirectory(),"persons.xml"); FileOutputStream fos = new FileOutputStream(path); //指定序列化对象输出的位置和编码 serializer.setOutput(fos,"utf-8"); //写开始<?xml version='1.0' encoding='utf-8' standalone='yes' ?> serializer.startDocument("utf-8", true);
//<persons> serializer.startTag(null, "persons"); for (Person person : personList) { //开始写入 serializer.startTag(null, "person"); serializer.attribute(null, "id", String.valueOf(person.getId()));
//写名字<name> serializer.startTag(null, "name"); serializer.text(person.getName()); //</name> serializer.endTag(null, "name");
//写年龄 //<age> serializer.startTag(null, "age"); serializer.text(String.valueOf(person.getAge())); //</age> serializer.endTag(null, "age");
//</persons> serializer.endTag(null, "person"); }
//</persons> serializer.endTag(null, "persons");
//结束 serializer.endDocument(); } catch (Exception e) { e.printStackTrace(); } }
/** * 用于生成一个List的Person集合 * @return */ private List<Person> getPersonList() { List<Person> personList = new ArrayList<Person>();
for (int i = 0; i < 30; i++) { personList.add(new Person(i,"wang" + i, 18 + i)); }
return personList; }
/** * 解析xml文件并将xml文件中内容写List集合中 * @return */ private List<Person> parseXmlFromLocal() { try { File path = new File(Environment.getExternalStorageDirectory(),"persons.xml"); FileInputStream fis = new FileInputStream(path);
//获得pull解析器对象 XmlPullParser parser = Xml.newPullParser(); //指定解析的文件的编码格式 parser.setInput(fis,"utf-8");
//获得时间类型 int eventType = parser.getEventType();
List<Person> personList = null; Person person = null; String id;
while (eventType != XmlPullParser.END_DOCUMENT) { //获取当前节点的名称 String tagName = parser.getName();
switch (eventType) { //获得当前节点的名称 <person> case XmlPullParser.START_TAG: //<persons> if("persons".equals(tagName)) { personList = new ArrayList<Person>(); } else if ("person".equals(tagName)) { //<person id="1"> person = new Person(); id = parser.getAttributeValue(null,"id"); person.setId(Integer.valueOf(id)); } else if("name".equals(tagName)) { person.setName(parser.nextText()); } else if("age".equals(tagName)) { //<age> person.setAge(Integer.parseInt(parser.nextText())); } break; //</persons> case XmlPullParser.END_TAG: if ("person".equals(tagName)) { //需要把上面设置好的person对象添加到集合中 personList.add(person); } break; default: break; }
//获得下一个事件类型 eventType = parser.next(); } return personList; } catch (Exception e) { e.printStackTrace(); } return null; } } |
5 设置Log过滤器
6、控制台输出结果