android Pull方式解析xml文件


xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<persons>
	<person id="1">
		<name>zhangsan</name>
		<age>11</age>
	</person>
	<person id="2">
		<name>lisi</name>
		<age>18</age>
	</person>
	<person id="3">
		<name>wangwu</name>
		<age>20</age>
	</person>
</persons>

解析代码如下:

package com.pdsu.file.xml;

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;

/**
 * 采用pull方式解析xml文件
 * 
 * @author huangyinge
 * 
 */
public class PersonXML {

	/**
	 * 读取xml文件
	 * 
	 * @param is
	 *            xml文件输入流
	 * @return person列表
	 * @throws XmlPullParserException
	 */
	public List<Person> readXML(InputStream is) throws Exception {
		List<Person> persons = null;
		Person person = null;
		// 得到xmlpull解析器
		XmlPullParser xmlPull =XmlPullParserFactory.newInstance().newPullParser();
		xmlPull.setInput(is, "UTF-8");
		int eventType = xmlPull.getEventType();
		// 循环遍历每个元素只到文档末尾
		while (eventType != XmlPullParser.END_DOCUMENT) {
			switch (eventType) {
				case XmlPullParser.START_DOCUMENT:// 文档开始,只执行一次
					persons = new ArrayList<Person>();
					break;
				case XmlPullParser.START_TAG: {// 标签开始
					if ("person".equals(xmlPull.getName())) {
						int id = Integer.parseInt(xmlPull.getAttributeValue(0));
						person = new Person();
						person.setId(id);
					}
					if ("name".equals(xmlPull.getName())) {
						String name = xmlPull.nextText();
						person.setName(name);
					}
					if ("age".equals(xmlPull.getName())) {
						int age = Integer.parseInt(xmlPull.nextText());
						person.setAge(age);
					}
					break;
				}
				case XmlPullParser.END_TAG: {
					if ("person".equals(xmlPull.getName())) {
						persons.add(person);
						person =null;
					}
					break;
				}
			}
			eventType = xmlPull.next();
		}
		return persons;
	}
	/**
	 * 保存xml文件
	 * @param persons 
	 * @throws Exception
	 */
	public void saveXML(List<Person> persons,OutputStream os) throws Exception{
		XmlSerializer xmlSerializer = XmlPullParserFactory.newInstance().newSerializer();
		xmlSerializer.setOutput(os,"UTF-8");
		xmlSerializer.startDocument("UTF-8", true);
		xmlSerializer.startTag(null, "persons");
		for(Person person:persons){
			xmlSerializer.startTag(null, "person");
			xmlSerializer.attribute(null, "id", String.valueOf(person.getId()));
			
			xmlSerializer.startTag(null, "name");
			xmlSerializer.text(person.getName());
			xmlSerializer.endTag(null, "name");
			
			xmlSerializer.startTag(null, "age");
			xmlSerializer.text(String.valueOf(person.getAge()));
			xmlSerializer.endTag(null, "age");
			
			xmlSerializer.endTag(null, "person");
		}
		xmlSerializer.endTag(null, "persons");
		xmlSerializer.endDocument();
		os.flush();
		os.close();
	}
	
}
测试类代码:

package com.pdsu.file.xml;

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 org.junit.Test;

import android.test.AndroidTestCase;

import com.pdsu.file.xml.Person;
import com.pdsu.file.xml.PersonXML;

public class PersonXMLTest extends AndroidTestCase{
	@Test
	public void testReadXML() throws Exception{
			InputStream is = this.getClass().getClassLoader().getResourceAsStream("person.xml");
			PersonXML personXml = new PersonXML();
			List<Person> persons = personXml.readXML(is);
			for(Person person :persons){
				System.out.println(person);
			}
	}
	
	@Test 
	public void save() throws Exception{
		List<Person> persons = new ArrayList<Person>();
		persons.add(new Person(1,"name1",11));
		persons.add(new Person(2,"name2",112));
		persons.add(new Person(3,"name3",113));
		persons.add(new Person(4,"name4",114));
		PersonXML personXml = new PersonXML();
		FileOutputStream fos = new FileOutputStream(getContext().getFilesDir()+File.separator+"person.xml"); 
		personXml.saveXML(persons, fos);
	}
}



你可能感兴趣的:(android Pull方式解析xml文件)