在android中使用Pull解析器读取xml文件,并进行测试

1:读取xml文件的类

public class PullXMLService {
	
	/*
	 * <persons>
    <person id="28">
        <name>bo</name>
        <age>28</age>
    </person>
    <person id="29">
         <name>xiaohong</name>
         <age>21</age>
    </person>
    </persons>
	 */
	public List<Person> getPersons(InputStream inputStream) throws Throwable {
		List<Person> persons = null;
		Person person = null;
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(inputStream, "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)));  //得到ID的值
				} 
                if(person != null) {
                	if("name".equals(name)) {
                		person.setName(parser.nextText());   //得到name的值,即获取解析器当前指向元素的下一个文本节点的值
                	} else if("age".equals(name)) {
                		person.setAge(new Short(parser.nextText()));   //得到age的值
                	}
                }
                break;
			case XmlPullParser.END_TAG:
				if("person".equals(parser.getName())) {   //又到person的时候就结束一次赋值
					persons.add(person);
					person = null;
				}
				break;
			}
			eventType = parser.next();
		}
		return persons;
	}
}


2:测试程序

	public void testPullGetPersons() {
		PullXMLService pullXMLService = new PullXMLService();
		InputStream inputStream = getClass().getClassLoader().getResourceAsStream("test.xml");
		try {
			List<Person> persons = pullXMLService.getPersons(inputStream);
			for(Person person : persons) {
				Log.i(TAG, person.toString());
			}
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}


3:用到的Person类

public class Person {
	
	private Integer id;
	private String name;
	private Short age;
	
	public Person(){}

	public Person(Integer id, String name, Short age) {
		super();
		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 + "]";
	}

}

4:test.xml文件的内容

<?xml version="1.0" encoding="UTF-8"?>
<persons>
    <person id="28">
        <name>zhangbo</name>
        <age>28</age>
    </person>
    <person id="29">
         <name>xiaohong</name>
         <age>21</age>
    </person>
</persons>


5:测试结果

     在LogCat中打印输出:

                            Person:[age=28, id=28, name=bo]

                            Person:[age=21, id=29, name=xiaohong]

你可能感兴趣的:(在android中使用Pull解析器读取xml文件,并进行测试)