在Android应用中使用Pull解析XML文件(传智播客视频笔记)

Service.java源码:

package com.sinaapp.ssun.service;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;

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

import android.util.Xml;

import com.sinaapp.ssun.domain.Person;

public class Service {
	/**
	 * 获取XML文件中的数据
	 * @param xml
	 * @return
	 * @throws Exception
	 */
	public static List<Person> getPersons(InputStream xml) throws Exception {
		List<Person> persons = null;
		XmlPullParser parser = XmlPullParserFactory.newInstance()
				.newPullParser();
		// parser = Xml.newPullParser();
		parser.setInput(xml, "UTF-8");
		int event = parser.getEventType();
		Person p = null;
		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())){
					p = new Person();
					int id = Integer.parseInt(parser.getAttributeValue(0));
					p.setId(id);
				}
				if("name".equals(parser.getName())){
					String name = parser.nextText();
					p.setName(name);
				}
				if("age".equals(parser.getName())){
					int age = Integer.parseInt(parser.nextText());
					p.setAge(age);
				}
				break;
			case XmlPullParser.END_TAG:
				if("person".equals(parser.getName())){
					persons.add(p);
					p = null;
				}
				break;
			}
			event = parser.next();
		}
		return persons;
	}
	
	/**
	 * 保存数据到XML文件中
	 * @param persons
	 * @param out
	 * @throws Exception
	 */
	public static void save(List<Person> persons , OutputStream out) throws Exception{
		XmlSerializer serializer  =  Xml.newSerializer();
		serializer.setOutput(out, "UTF-8");
		serializer.startDocument("UTF-8", true);
		serializer.startTag(null, "persons");
		for(Person p: persons){
			serializer.startTag(null, "person");
			serializer.attribute(null, "person", p.getId()+"");
			
			serializer.startTag(null, "name");
			serializer.text(p.getName());
			serializer.endTag(null, "name");
			
			serializer.startTag(null, "age");
			serializer.text(p.getAge()+"");
			serializer.endTag(null, "age");
			
			serializer.endTag(null, "person");
		}
		serializer.endTag(null, "persons");
		serializer.endDocument();
		out.flush();
		out.close();
	}
}



Person.java源码:

package com.sinaapp.ssun.domain;

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

text.xml文件:

<!--test.xml-->
<?xml version="1.0" encoding="UTF-8"?><!-- 开始文档语法 -->
<persons>
    	<person  id="1">
    	    <name>ssun</name>
    	    <age>19</age>
    	</person>
    	<person  id="2">
    	    <name>cobe</name>
    	    <age>24</age>
    	</person>
</persons>

单元测试TestService.java源码:

package com.sinaapp.ssun.test;

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

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

import com.sinaapp.ssun.domain.Person;
import com.sinaapp.ssun.service.Service;

public class TestService extends AndroidTestCase {
	private final String Tag = "Test";
	
	public void testPersons() throws Exception{
		List<Person> persons = Service.getPersons(this.getClass().getClassLoader().getResourceAsStream("test.xml"));
		for(Person p : persons){
			Log.i(Tag, p.toString());
		}
	}
	
	public void testSave() throws Exception{
		List<Person> persons = new ArrayList<Person>();
		persons.add(new Person("www",19,23));
		persons.add(new Person("hhh",19,3));
		persons.add(new Person("qqq",19,24)); 
		persons.add(new Person("ooo",19,25));
		File file = new File(this.getContext().getFilesDir(),"test2.xml");
		FileOutputStream out = new FileOutputStream(file);
		Service.save(persons, out);
	}
	
}
  

你可能感兴趣的:(xml,android,exception,String,File,null)