码迷,mamicode.com
首页 > 移动开发 > 详细

android pull解析XML文件

时间:2014-11-16 13:28:00      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:android   io   ar   os   java   for   文件   on   art   

package com.example.pullxml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

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

import android.os.Environment;
import android.test.AndroidTestCase;
import android.util.Log;
import android.util.Xml;

public class PullXml extends AndroidTestCase{
	public void test() {
		writeXML();
		List<Person> personList = parserXML();
		for (Person person : personList) {
			Log.i("id", String.valueOf(person.getId()));
			Log.i("name", person.getName());
			Log.i("age", String.valueOf(person.getAge()));
		}
	}

	public void writeXML() {
		// 获得序列化对象
		XmlSerializer serializer = Xml.newSerializer();
		List<Person> personList = getPersonList();
		try {
			File sdDir = Environment.getExternalStorageDirectory();
			File file = new File(sdDir, "persons.xml");
			FileOutputStream fos = new FileOutputStream(file);
			// 指定输出位置和编码
			serializer.setOutput(fos, "utf-8");
			serializer.startDocument("utf-8", true);// <?xml version='1.0'
													// encoding='utf-8'
													// standalone='true' ?>
			serializer.startTag(null, "persons");// <persons>
			for (Person person : personList) {
				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(String.valueOf(person.getAge()));
				serializer.endTag(null, "age");

				serializer.endTag(null, "person");
			}
			serializer.endTag(null, "persons");// </persons>
			serializer.endDocument();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<Person> parserXML() {
		List<Person> personList = null;
		Person person = null;
		Long id = null;
		// 获得pull解析对象
		XmlPullParser parser = Xml.newPullParser();
		try {
			File sdDir = Environment.getExternalStorageDirectory();
			File file = new File(sdDir, "persons.xml");
			FileInputStream fis = new FileInputStream(file);
			parser.setInput(fis, "utf-8");
			// 获得事件类型
			int eventType = parser.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {
				String tagName = parser.getName();// 获得当前节点名称
				switch (eventType) {
				case XmlPullParser.START_TAG:
					if ("persons".equals(tagName)) {
						personList = new ArrayList<Person>();
					} else if ("person".equals(tagName)) {
						person = new Person();
						id = Long.parseLong(parser
								.getAttributeValue(null, "id"));
						person.setId(id);
					} else if ("name".equals(tagName)) {
						person.setName(parser.nextText());
					} else if ("age".equals(tagName)) {
						person.setAge(Integer.parseInt(parser.nextText()));
					}
					break;
				case XmlPullParser.END_TAG:
					if("person".equals(tagName)){
						personList.add(person);
					}
					break;

				}
				eventType=parser.next();

			}

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return personList;
	}

	public List<Person> getPersonList() {
		List<Person> personList = new ArrayList<Person>();
		for (int i = 0; i < 10; i++) {
			Person person = new Person((long) i, "liang" + i, i);
			personList.add(person);
		}
		return personList;
	}
}

class Person {
	private Long id;
	private String name;
	private Integer age;

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

	public Person() {
		super();
	}

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

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

}

android pull解析XML文件

标签:android   io   ar   os   java   for   文件   on   art   

原文地址:http://blog.csdn.net/liang5630/article/details/41171917

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!