安卓学习笔记--读写xml文件

时间:2023-02-01 22:44:13

步骤一:建个Person.java

package com.example.analysisxml;

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;
}
public Person(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}

public Person(){}
}

步骤二:建xml操作类 

package com.example.analysisxml;

import java.io.*;
import java.util.*;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import android.util.Xml;
public class PersonService {
public List<Person> GetPersons(InputStream xmlstream) throws Exception{
List<Person> persons=null;
XmlPullParser pull=Xml.newPullParser();
pull.setInput(xmlstream, "UTF-8");//为pull解析器设置要解析的xml数据
int event=pull.getEventType();
Person p=null;
while(event!=XmlPullParser.END_DOCUMENT)
{
switch(event)
{
case XmlPullParser.START_DOCUMENT:
persons=new ArrayList<Person>();
break;
case XmlPullParser.START_TAG:
if("person".equals(pull.getName()))
{
int id=new Integer(pull.getAttributeValue(0));
p=new Person();
p.setId(id);
}
if("name".equals(pull.getName()))
{
String name=pull.nextText();
if(p==null)p=new Person();
p.setName(name);
}
if("age".equals(pull.getName()))
{
int age=new Integer(pull.nextText());
if(p==null)p=new Person();
p.setAge(age);
}
break;
case XmlPullParser.END_DOCUMENT:
if("person".equals(pull.getName()))
{
persons.add(p);
}
break;
}
event=pull.next();
}
return persons;
}

public void SavePersonToXml(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, "id", person.getId().toString());

serializer.startTag(null, "name");
serializer.text(person.getName());
serializer.endTag(null, "name");

serializer.startTag(null, "age");
serializer.text(person.getAge().toString());
serializer.endTag(null, "age");

serializer.endTag(null, "person");
}
serializer.endTag(null, "persons");
serializer.endDocument();
out.flush();
out.close();
}
}

步骤三:在需要调用的地方调用上面的类,本例是在测试类中进行的

<pre name="code" class="java">package com.example.analysisxml.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import com.example.analysisxml.Person;
import com.example.analysisxml.PersonService;

import android.test.AndroidTestCase;
import android.util.Log;

public class xmlTest extends AndroidTestCase {
private static final String tag="readxml";
public void ReadXml() throws Throwable
{
PersonService ps=new PersonService();
InputStream xmlstream=this.getClass().getClassLoader().getResourceAsStream("person.xml");
List<Person> list=ps.GetPersons(xmlstream);
Log.i(tag, "开始读取xml文件");
if(list!=null){
for(Person p:list){
Log.i(tag, "姓名:"+p.getName()+";年龄:"+p.getAge()+";编号:"+p.getId());
}
}
}

public void Savexml() throws Exception{
List<Person> list=new ArrayList<Person>();
list.add(new Person(20,"zhangsan",21));
list.add(new Person(10,"lisi",23));
list.add(new Person(14,"wangwu",30));
PersonService ps=new PersonService();
File file=new File(getContext().getFilesDir(),"myperson.xml");
OutputStream out=new FileOutputStream(file);
ps.SavePersonToXml(list, out);
}
}