android PULL解析XML

Pull解析和Sax解析很相似,都是轻量级的解析,在Adnroid的内核中已经嵌入了XML Pull Parsing提供的org.xmlpull.*

包,所以不需要再添加第三方jar包来支持Pull。PULL解析XML的方式与SAX解析XML方式一样,也是基于事件驱动

的.

得到一个解析器:XmlPullParser parser = Xml.newPullParser();

传进一个输入流:parser.setInput(inStream, "UTF-8");

得到事件类 型:parser.getEventType();

判断是否是文件结尾:XmlPullParser.END_DOCUMENT

判断是否 是文件开始:XmlPullParser.START_DOCUMENT:

判断是否是标签开始:XmlPullParser.START_TAG

判断是否是标签结束:XmlPullParser.END_TAG

将指针往下推:parser.next();


xmlPullParser中定义了常量来标识各种解析事件:

常量 常量所标识的解析事件
START_DOCUMENT 读取到XML的声明返回
END_DOCUMENT 读取到XML的结束返回
START_TAG 读取到XML的开始标签返回
END_TAG 读取到XML的结束标签返回
TEXT 读取到XML的文本返回

domain:

package com.wangjialin.domain;

public class Person {
	private Integer id;
	private String name;
	private Short age;

	public Person() {
	}

	public Person(Integer id, String name, Short age) {
		this.id = id;
		this.name = name;
		this.age = age;
	}

	public Person(String name, Short age) {
		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;
	}

	@Override
	public String toString() {
		return "id=" + id + ",name=" + name + ",age=" + age;
	}

}

service:

package com.wangjialin.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.wangjialin.domain.Person;

public class PullPersonService {
	public static void save(List<Person> persons, OutputStream outStream)
			throws Exception {
		XmlSerializer serializer = Xml.newSerializer();
		serializer.setOutput(outStream, "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();
		outStream.flush();
		outStream.close();
	}

	public static List<Person> getPersons(InputStream inStream)
			throws Exception {
		Person person = null;
		List<Person> persons = null;
		XmlPullParser pullParser = Xml.newPullParser();
		pullParser.setInput(inStream, "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 (person != null) {
					if ("name".equals(pullParser.getName())) {
						person.setName(pullParser.nextText());
					}
					if ("age".equals(pullParser.getName())) {
						person.setAge(new Short(pullParser.nextText()));
					}
				}
				break;

			case XmlPullParser.END_TAG:
				if ("person".equals(pullParser.getName())) {
					persons.add(person);
					person = null;
				}
				break;
			}
			event = pullParser.next();
		}
		return persons;
	}
}


Activity或test类:
package com.wangjialin.xml;

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

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

import com.wangjialin.domain.Person;

import com.wangjialin.service.PullPersonService;

public class PersonServiceTest extends AndroidTestCase {
	private static final String TAG = "PersonServiceTest";

	public void testPullgetPersons() throws Throwable {
		InputStream inStream = this.getClass().getClassLoader()
				.getResourceAsStream("wangjialin.xml");
		List<Person> persons = PullPersonService.getPersons(inStream);
		for (Person person : persons) {
			Log.i(TAG, person.toString());
		}
	}

	public void testSave() throws Throwable {
		File file = new File(this.getContext().getFilesDir(), "wangjialin.xml");
		FileOutputStream outStream = new FileOutputStream(file);
		List<Person> persons = new ArrayList<Person>();
		persons.add(new Person(90, "wangjialin", (short) 13));
		persons.add(new Person(35, "jialingege", (short) 23));
		persons.add(new Person(78, "Android", (short) 33));
		PullPersonService.save(persons, outStream);
	}
}


你可能感兴趣的:(android)