采用Pull解析器处理XML数据

1、person.xml

<?xml version="1.0" encoding="UTF-8"?>
<persons>
	<person id="001">
		<name>张三</name>
		<sex>男</sex>
		<age>23</age>
	</person>
	<person id="002">
		<name>梅超风</name>
		<sex>女</sex>
		<age>22</age>
	</person>
</persons>

 2、PersonService.java

package com.webyun.app.service;

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.XmlSerializer;

import android.util.Xml;

import com.webyun.app.domain.Person;

public class PersonService {

	/**
	 * 获取数据
	 * @param inputStream
	 * @return
	 * @throws Exception
	 */
	public static List<Person> getPersons(InputStream inputStream) throws Exception {
		List<Person> persons = null;
		Person person = null;
		//通过Android帮助类获取Pull解析器
		XmlPullParser pullParser = Xml.newPullParser();
		
		//为Pull解析器设置需要解析的XML数据
		pullParser.setInput(inputStream, "UTF-8");
		
		int eventType = pullParser.getEventType();
		
		while(eventType != XmlPullParser.END_DOCUMENT){
			switch(eventType){
			case XmlPullParser.START_DOCUMENT:
				persons = new ArrayList<Person>();
				break;
			case XmlPullParser.START_TAG:
				if("person".equals(pullParser.getName())){
					person = new Person();
					String id = pullParser.getAttributeValue(0);
					person.setId(id);
				}
				if("name".equals(pullParser.getName())){
					String name = pullParser.nextText();
					person.setName(name);
				}
				if("sex".equals(pullParser.getName())){
					String sex = pullParser.nextText();
					person.setSex(sex);
				}
				if("age".equals(pullParser.getName())){
					int age = new Integer(pullParser.nextText());
					person.setAge(age);
				}
				break;
			case XmlPullParser.END_TAG:
				if("person".equals(pullParser.getName())){
					persons.add(person);
					person = null;
				}
				break;
			}
			eventType = pullParser.next();
		}
		return persons;
	}
	
	/**
	 * 保存数据
	 * @param persons	数据
	 * @param os		输出方向
	 * @throws Exception
	 */
	public static void save(List<Person> persons, OutputStream os) throws Exception {
		XmlSerializer serializer = Xml.newSerializer();
		serializer.setOutput(os, "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, "sex");
			serializer.text(person.getSex());
			serializer.endTag(null, "sex");
			
			serializer.startTag(null, "age");
			serializer.text(person.getAge().toString());
			serializer.endTag(null, "age");
			
			serializer.endTag(null, "person");
		}
		
		serializer.endTag(null, "persons");
		
		//结束文档
		serializer.endDocument();
		
		os.flush();
		os.close();
	}
}

 

 3、PersonServiceTest.java

package com.webyun.app.test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

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

import com.webyun.app.domain.Person;
import com.webyun.app.service.PersonService;

public class PersonServiceTest extends AndroidTestCase {
	private static final String TAG = "PersonServiceTest";
	public void testPersons() throws Exception {
		InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("person.xml");
		List<Person> persons = PersonService.getPersons(inputStream);
		for(Person person :persons){
			Log.i(TAG, person.toString());
		}
	}
	
	public void testSave() throws Exception {
		List<Person> persons = new ArrayList<Person>();
		persons.add(new Person("003","李四","男",25));
		persons.add(new Person("004","大雄","男",22));
		persons.add(new Person("005","豆豆","女",20));
		
		PersonService.save(persons, System.out);
	}
}

 

 

你可能感兴趣的:(pull)