Android原始XML的读写操作
代码如下:
package com.example.xml2;
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.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;
import com.xh.tx.bean.Person;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.util.Xml;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Person> list = new ArrayList<Person>();
Person p = null;
for(int i=0; i <10; i++ )
{
p = new Person(i,"zs" + i,"32432432@qq.com","西安南路" + i+ "号");
list.add(p);
}
writePerson(list);
readPerson();
}
/**
* 有一个List<Person> list ;
* 把list里面的所有对象序列化到sdcard里面形成一个xml文件
*/
public void writePerson(List<Person> list)
{
if(null == list)
{
return;
}
XmlSerializer serializer = Xml.newSerializer();
File path = new File(Environment.getExternalStorageDirectory(),"list_person.xml");
try {
FileOutputStream out = new FileOutputStream(path);
serializer.setOutput(out, "utf-8");
//开始序列化xml
serializer.startDocument("utf-8", true);
//根
serializer.startTag(null, "persons");
for(Person p : list)
{
serializer.startTag(null, "person");
serializer.attribute(null, "id", String.valueOf(p.getId()));
//推进使用String.valueOf 将其他类型的对象转化为string对象
serializer.startTag(null, "name");
serializer.text(p.getName()); //<name>allen</name>
serializer.endTag(null, "name");
serializer.startTag(null, "email");
serializer.text(p.getEmail());
serializer.endTag(null, "email");
serializer.startTag(null, "address");
serializer.text(p.getAddress());
serializer.endTag(null, "address");
serializer.endTag(null, "person");
}
serializer.endTag(null, "persons");
serializer.endDocument();
} catch (Exception e) {
e.printStackTrace();
}
}
public void readPerson()
{
//工具类 我们称这种方式为pull解析(DOM,SAX,DOM4J)
XmlPullParser parser = Xml.newPullParser();
try {
FileInputStream in = new FileInputStream(new File(Environment.getExternalStorageDirectory(),"list_person.xml"));
parser.setInput(in, "utf-8");//设置你要解析那个文件,已什么编码格式把它读入到内存
//下面开始解析xml
int eventType = parser.getEventType();
List<Person> list = null;
Person p = null;
while(eventType != parser.END_DOCUMENT)
{
switch (eventType) {
case XmlPullParser.START_TAG: //访问静态变量的时候用类名来访问,不要用对象去调用
if("persons".equals(parser.getName()))
{
list = new ArrayList<Person>();
}else if("person".equals(parser.getName())) //当遇到了person节点的时候创建person对象并解析id属性给person对象
{
p = new Person();
int id = Integer.parseInt(parser.getAttributeValue(null, "id"));
p.setId(id);
}else if("name".equals(parser.getName()))
{
String name = parser.nextText(); //一般用nextText 不用getText
p.setName(name);
}else if("email".equals(parser.getName()))
{
String email = parser.nextText(); //一般用nextText 不用getText
p.setEmail(email);
}else if("address".equals(parser.getName()))
{
String address = parser.nextText(); //一般用nextText 不用getText
p.setAddress(address);
}
break;
case XmlPullParser.END_TAG:
if("person".equals(parser.getName()))//表示person结束了,将person对象加入到list中
{
list.add(p);
p = null; //及时的让我们不用的对象成为垃圾
}
break;
default:
break;
}
eventType = parser.next(); //尤其容易忘记
}
//打印一下解析出来的所有对象
for(Person person : list)
{
Log.d(TAG, person.toString());
}
} catch (XmlPullParserException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Person类代码:
package com.xh.tx.bean;
public class Person
{
private int id;
private String name;
private String email;
private String address;
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 String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Person() {
super();
}
public Person(int id, String name, String email, String address) {
super();
this.id = id;
this.name = name;
this.email = email;
this.address = address;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", email=" + email
+ ", address=" + address + "]";
}
}
还需要在 AndroidManifest.xml 中注册
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>