android基础--使用Pull解析器生成XML文件

 

解析person.xml文件:

person.xml

<?xml version="1.0" encoding="UTF-8"?>
<persons>
	<person id="23">
		<name>张三</name>
		<age>3</age>
	</person>
	<person id="20">
		<name>Mary</name>
		<age>25</age>
	</person>
</persons>


1.Manifest.xml中增加

<uses-library android:name="android.test.runner" />

<instrumentation android:name="android.test.InstrumentationTestRunner"

              android:targetPackage="cn.com.xml" android:label="Tests for My App" />

建立person.xml对应的javabean类

public class Person {
	private String name;
	private int id;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Person [age=" + age + ", id=" + id + ", name=" + name + "]";
	}
	
}


2.业务层中解析person.xml

public static ArrayList<Person> getPersons(InputStream xml)
			throws Exception {
		ArrayList<Person> persons = null;
		Person person = null;
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(xml, "utf-8");
		int event = parser.getEventType();
		while (event != XmlPullParser.END_DOCUMENT) {
			switch (event) {
			case XmlPullParser.START_DOCUMENT:
				persons = new ArrayList<Person>();
				break;
			case XmlPullParser.START_TAG:
				if ("person".equals(parser.getName())) {
					person = new Person();
					person.setId(Integer.parseInt(parser.getAttributeValue(0)));
				} else if ("name".equals(parser.getName())) {
					person.setName(parser.nextText());
				} else if ("age".equals(parser.getName())) {
					person.setAge(Integer.parseInt(parser.nextText()));
				}
				break;
			case XmlPullParser.END_TAG:
				if ("person".equals(parser.getName())) {
					persons.add(person);
					person = null;
				}
			default:
				break;
			}
			event = parser.next();
		}
		return persons;
	}


3.测试

public void testGetPersons() throws Throwable{
		InputStream xml = this.getClass().getClassLoader().getResourceAsStream("person.xml");
		ArrayList<Person> al = PullXml.getPersons(xml);
		for(Person person: al){
			Log.i("PersonTest", person.toString());
		}
	}


集合xml的转换,将例子中类路径下的person.xml文件读取并存放到手机中应用程序的/data/data/应用程序包/files下

1.业务层方法:

public static void savePersons(ArrayList<Person> persons, OutputStream out)throws Exception{
		XmlSerializer xs = Xml.newSerializer();
		xs.setOutput(out, "utf-8");
		xs.startDocument("utf-8", true);
		xs.startTag(null, "persons");
		for(Person p : persons){
			xs.startTag(null, "person");
			xs.attribute(null, "id", p.getId()+"");
			
			xs.startTag(null, "name");
			xs.text(p.getName());
			xs.endTag(null, "name");
			xs.startTag(null, "age");
			xs.text(p.getAge()+"");
			xs.endTag(null, "age");
			xs.endTag(null, "person");
		}
		xs.endTag(null, "persons");
		xs.endDocument();
		out.flush();
		out.close();
	}


2.测试

public void testSavePersons() throws Throwable{
		InputStream xml = this.getClass().getClassLoader().getResourceAsStream("person.xml");
		ArrayList<Person> al = PullXml.getPersons(xml);
		FileOutputStream out = new FileOutputStream(new File(getContext().getFilesDir(),"zzzz.xml"));
		PullXml.savePersons(al, out);
	}


 

阅读更多
个人分类: android学习
想对作者说点什么? 我来说一句

Android解析xml文件的3种方式.doc

2014年01月15日 322KB 下载

Android利用pull生成xml文件

2013年10月28日 982KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭