android——写xml

在PersonService的基础上,加上savePersons(·····),这时的PersonService为:

 

package com.njupt.xml;



import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.ArrayList;

import java.util.List;



import org.xmlpull.v1.XmlPullParser;

import org.xmlpull.v1.XmlPullParserException;

import org.xmlpull.v1.XmlSerializer;



import android.util.Xml;



public class PersonService {



	public List<Person> loadPersons(InputStream in) throws Exception{

		XmlPullParser parser = Xml.newPullParser();

		parser.setInput(in,"UTF-8");

		

		List<Person> persons = new ArrayList<Person>();

		Person p = null;

		

		for(int type = parser.getEventType() ; type != XmlPullParser.END_DOCUMENT ; type = parser.next()){

			

			if(type == XmlPullParser.START_TAG){

				if(parser.getName().equals("person")){

					p = new Person();

					String id = parser.getAttributeValue(0);

					p.setId(Integer.parseInt(id));

					persons.add(p);

					

				}else if(parser.getName().equals("name")){

					String name = parser.nextText();

					p.setName(name);

				}else if(parser.getName().equals("age")){

					String age = parser.nextText();

					p.setAge(Integer.parseInt(age));

				}

			}

		}

		return persons;

	}



	public void savePersons(List<Person> persons,

			FileOutputStream out) throws Exception {



		XmlSerializer serializer = Xml.newSerializer();

		serializer.setOutput(out,"UTF-8");

		

		serializer.startDocument("UTF-8", true);

		serializer.startTag(null, "persons");

		

		for(Person p : persons){

			serializer.startTag(null, "person");

			serializer.attribute(null, "id", p.getId().toString());

			

			serializer.startTag(null, "name");

			serializer.text(p.getName());

			serializer.endTag(null, "name");

			

			serializer.startTag(null, "age");

			serializer.text(p.getAge().toString());

			serializer.endTag(null, "age");

			

			

			serializer.endTag(null, "person");

		}

		serializer.endTag(null, "persons");

		serializer.endDocument();

		

	}

}


2、PersonTest

 

 

package com.njupt.xml;



import java.io.FileOutputStream;

import java.io.InputStream;

import java.util.List;



import android.test.AndroidTestCase;



public class PersonTest extends AndroidTestCase{



	public void test1() throws Exception{

		PersonService service = new PersonService();

		InputStream in = PersonTest.class.getClassLoader().getResourceAsStream("persons22.xml");

	    

		List<Person> persons = service.loadPersons(in);

	 

		for(Person p : persons){

			System.out.println(p);

		}

		

		Person p = new Person(4,"allen",20);

		persons.add(p);

		

		service.savePersons(persons,new FileOutputStream("/mnt/sdcard/persons22.xml"));

	}

}


3、Person22.xml

 

这时生成的person22.xml文件的内容如下(注意,他不会帮我们把xml文件格式化):

 

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><persons><person id="1"><name>范冰冰</name><age>31</age></person><person id="2"><name>林志玲</name><age>38</age></person><person id="3"><name>杨幂</name><age>26</age></person><person id="4"><name>allen</name><age>20</age></person></persons>


 


 

你可能感兴趣的:(android)