首页  >  文章  >  后端开发  >  具体分析Pull方式解析XML的示例代码

具体分析Pull方式解析XML的示例代码

黄舟
黄舟原创
2017-03-31 14:42:581661浏览

Pull方式解析XML 
1、XML文件 

<?xml version="1.0" encoding="UTF-8"?>
<persons>

	<person id="23">
		<name>liming</name>
		<age>30</age>
	</person>
	<person id="20">
		<name>lixiangmei</name>
		<age>25</age>
	</person>
</persons>

2、实体类 

package com.example.pullparsexml;

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 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 "Person [age=" + age + ", id=" + id + ", name=" + name + "]";
	}
	
}

3、代码实现 

package com.example.pullparsexml;

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;

public class PullParseXML {
	public static void save(List<Person> persons, Writer writer)
			throws Throwable {
		XmlSerializer serializer = Xml.newSerializer();
		serializer.setOutput(writer);
		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();
		writer.flush();
		writer.close();
	}

	public static List<Person> getPersons(InputStream inStream)
			throws Throwable {
		List<Person> persons = null;
		Person person = null;
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(inStream, "UTF-8");
		int eventType = parser.getEventType();// 产生第一个事件
		while (eventType != XmlPullParser.END_DOCUMENT) {// 只要不是文档结束事件
			switch (eventType) {
			case XmlPullParser.START_DOCUMENT:
				persons = new ArrayList<Person>();
				break;

			case XmlPullParser.START_TAG:
				String name = parser.getName();// 获取解析器当前指向的元素的名称
				if ("person".equals(name)) {
					person = new Person();
					person.setId(new Integer(parser.getAttributeValue(0)));
				}
				if (person != null) {
					if ("name".equals(name)) {
						person.setName(parser.nextText());// 获取解析器当前指向元素的下一个文本节点的值
					}
					if ("age".equals(name)) {
						person.setAge(new Short(parser.nextText()));
					}
				}
				break;

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

以上是具体分析Pull方式解析XML的示例代码的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn