Pull解析和生成XML文件

1、XML

<?xml version="1.0" encoding="UTF-8"?>
<persons>
	<person id="23">
		<name>李明</name>
		<age>30</age>
	</person>
	<person id="20">
		<name>李向梅</name>
		<age>25</age>
	</person>
</persons>


2、JAVABean
package com.wynlink.domain;

public class Person {

	private Integer id;
	private String name;
	private Short age;
	
	public Person() {
		super();
	}
	public Person(String name, Short age) {
		super();
		this.name = name;
		this.age = age;
	}
	public Person(Integer id, String name, Short age) {
		super();
		this.id = id;
		this.name = name;
		this.age = 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 Short getAge() {
		return age;
	}
	public void setAge(Short age) {
		this.age = age;
	}

	//重写toString方法
	@Override
	public String toString() {
		return "id="+id+",name="+name+",age="+age;
	}
	
	
}


package com.wynlink.xml;

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

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

import com.wynlink.domain.Person;
import com.wynlink.service.DOMPersonService;
import com.wynlink.service.PullPersonService;
import com.wynlink.service.SAXPersonService;

/**
 * JUnit测试类
 * @author yaodaqing
 *
 */
public class XMLPersonServiceTest extends AndroidTestCase {

	public static final String TAG = "XMLPersonServiceTest";
	
	//使用Pull方法解析XML内容
	public void testPullReadXML() throws Exception{
		InputStream inStream = XMLPersonServiceTest.class.getClassLoader().getResourceAsStream("wynlink.xml");
		List<Person> persons = PullPersonService.readxml(inStream);
		for(Person person : persons){
			Log.i(TAG, person.toString());
		}
	}
	
	//使用Pull方法写入XML内容;写入到手机或者SD卡都可以,加上权限
	public void testPullWriterXML() throws Exception{
		/*1、不带缓冲功能的文件内容输出写法
		 * FileOutputStream outStream = this.getContext().openFileOutput("persons.xml", Context.MODE_WORLD_READABLE);
		OutputStreamWriter writer = new OutputStreamWriter(outStream,"UTF-8");//输出流写入器
		List<Person> persons = new ArrayList<Person>();
		persons.add(new Person(12,"daqing",new Short((short)27)));
		persons.add(new Person(13,"老毕",new Short((short)37)));
		persons.add(new Person(23,"姚大庆",new Short((short)26)));
		PullPersonService.writeXml(persons, writer);*/
		
		/*2、带缓冲功能的文件内容输出写法
		 * FileOutputStream outStream = this.getContext().openFileOutput("persons.xml", Context.MODE_WORLD_READABLE);
		OutputStreamWriter writer = new OutputStreamWriter(outStream,"UTF-8");
		BufferedWriter bufferedWriter = new BufferedWriter(writer);//带缓冲功能
		List<Person> persons = new ArrayList<Person>();
		persons.add(new Person(12,"daqing",new Short((short)27)));
		persons.add(new Person(13,"老毕",new Short((short)37)));
		persons.add(new Person(23,"姚大庆",new Short((short)26)));
		PullPersonService.writeXml(persons, bufferedWriter);//填写缓冲*/
		
		/*
		 * 3、不写入到文件里去
		 * 		只得到生成的完整的XML内容
		 */
		StringWriter writer = new StringWriter();//把数据输出到内存里
		List<Person> persons = new ArrayList<Person>();
		persons.add(new Person(12,"daqing",new Short((short)27)));
		persons.add(new Person(13,"老毕",new Short((short)37)));
		persons.add(new Person(23,"姚大庆",new Short((short)26)));
		PullPersonService.writeXml(persons, writer);
		Log.i(TAG, writer.toString());
	}
}



4、
package com.wynlink.service;

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

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

import android.util.Xml;

import com.wynlink.domain.Person;

/**
 * pull方式解析XML
 * @author yaodaqing
 *
 */
public class PullPersonService {
	
	/**
	 * 写数据到XML文件中
	 * 这里用Writer写入器。当然用流也可以
	 * 		利用StringBuffer或StringBuilder来拼装XML
	 * 此做法是面向对象的做法
	 */
	public static void writeXml(List<Person> persons,Writer writer) throws Exception{
		XmlSerializer serializer = Xml.newSerializer();
		serializer.setOutput(writer);//这里可以接受Writer和OutPutString
		serializer.startDocument("UTF-8", true);//开始文档
		serializer.startTag(null, "persons");//开始标签;如果没有命名空间,则填null
		for(Person person : persons){
			serializer.startTag(null, "person");
			serializer.attribute(null, "id", String.valueOf(person.getId()));
			
			serializer.startTag(null, "name");
			serializer.text(person.getName());
			serializer.endTag(null, "name");
			
			serializer.startTag(null, "age");
			serializer.text(String.valueOf(person.getAge()));
			serializer.endTag(null, "age");
			
			serializer.endTag(null, "person");
		}
		serializer.endTag(null, "persons");//结束标签
		serializer.endDocument();
		writer.flush();
		writer.close();
	}

	/**
	 * 从XML文件中读取数据
	 * @param inStream
	 * @return
	 * @throws Exception
	 */
	public static List<Person> readxml(InputStream inStream) throws Exception {
		List<Person> persons = null;
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(inStream, "UTF-8");
		int eventCode = parser.getEventType();//事件
		Person person = null;
		while(eventCode != XmlPullParser.END_DOCUMENT){//只要事件不等于结束标签,则一直向下读取
			switch (eventCode) {
			case XmlPullParser.START_DOCUMENT:	//文档开始事件
				persons = new ArrayList<Person>();
				break;
			case XmlPullParser.START_TAG:		//开始元素
				if("person".equals(parser.getName())){
					person = new Person();
					person.setId(new Integer(parser.getAttributeValue(0)));
				} else if(person != null){
					if("name".equals(parser.getName())){
						person.setName(parser.nextText());
					} else if("age".equals(parser.getName())){
						person.setAge(new Short(parser.nextText()));
					}
				}
				break;
			case XmlPullParser.END_TAG:			//结束元素
				if("person".equals(parser.getName()) && person != null){
					persons.add(person);
					person = null;
				}
				break;
			}
			eventCode = parser.next();
		}
		return persons;
	}
}

你可能感兴趣的:(xml,android,JUnit)