项目总体结构图
person.xml
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person id="1">
<age>19</age>
<name>张三</name>
</person>
<person id="2">
<age>20</age>
<name>王五</name>
</person>
</persons>
MainActivity
package com.xml2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Person
/**
*
*/
package com.zhou.entity;
/**
* @author zyw
* @Email zyw_205@gamil.com
* 2012-12-30
*/
public class Person {
private Integer id;
private Integer age;
private String name;
public Person() {
}
public Person(Integer id, Integer age, String name) {
super();
this.id = id;
this.age = age;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [id=" + id + ", age=" + age + ", name=" + name + "]";
}
}
PersonServer
/**
*
*/
package com.zhou.server;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer;
import android.util.Xml;
import com.zhou.entity.Person;
/**
* @author zyw
* @Email zyw_205@gamil.com
* 2012-12-30
*/
public class PersonServer {
/**
*
* @param xml
* @return
* @throws Exception
*/
public static List<Person> getPersons(InputStream xml) throws Exception{
List<Person> persons=null;
Person person=null;
XmlPullParser pullParser=Xml.newPullParser(); //XmlPullParserFactory.newInstance().newPullParser();
pullParser.setInput(xml, "UTF-8");
int event=pullParser.getEventType();
while(event!=XmlPullParser.END_DOCUMENT){
switch (event) {
case XmlPullParser.START_DOCUMENT:
persons=new ArrayList<Person>();
break;
case XmlPullParser.START_TAG:
if("person".equals(pullParser.getName())){
int id=new Integer(pullParser.getAttributeValue(0));
person=new Person();
person.setId(id);
}
if("age".equals(pullParser.getName())){
int age=new Integer(pullParser.nextText());
person.setAge(age);
}
if("name".equals(pullParser.getName())){
String name=pullParser.nextText();
person.setName(name);
}
break;
case XmlPullParser.END_TAG:
if("person".equals(pullParser.getName())){
persons.add(person);
person=null;
}
break;
}
event=pullParser.next();
}
return persons;
}
/**
* 保存数据
* @param persons 数据
* @param out 输出方向
* @throws Exception
*/
public static 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, "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();
}
}
/**
*
*/
package com.zhou.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import com.zhou.entity.Person;
import com.zhou.server.PersonServer;
import android.test.AndroidTestCase;
import android.util.Log;
import android.widget.AbsListView;
/**
* @author zyw
* @Email zyw_205@gamil.com
* 2012-12-30
*/
public class PersonServerTest extends AndroidTestCase{
private static final String tag="message";
public void testPerson() throws Exception{
InputStream xml=this.getClass().getClassLoader().getResourceAsStream("person.xml");
List<Person> persons=PersonServer.getPersons(xml);
for (Person person : persons) {
System.out.println(person.toString());
Log.i(tag, person.toString());
}
}
public void testsave() throws Exception{
List<Person> persons=new ArrayList<Person>();
persons.add(new Person(1, 12, "Luke"));
persons.add(new Person(2, 22, "张三"));
persons.add(new Person(3, 42, "周周"));
//<包>/files
File file=new File(getContext().getFilesDir(),"person2.xml");
FileOutputStream outStream=new FileOutputStream(file);
PersonServer.save(persons, outStream);
}
}