旗下导航:搜·么
当前位置:网站首页 > XML教程 > 正文

具体分析Pull体式格局剖析XML的示例代码【XML教程】,Pull,XML

作者:搜教程发布时间:2019-12-01分类:XML教程浏览:85评论:0


导读:Pull体式格局剖析XML1、XML文件<?xmlversion="1.0"encoding="UTF-8"?><persons>...
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的示例代码的细致内容,更多请关注ki4网别的相干文章!

标签:PullXML


欢迎 发表评论: