XML解析------XmlPullParser

XML解析------XmlPullParser

工程结构图:
[img]
XML解析------XmlPullParser_第1张图片
[/img]

运行效果图:
[img]

[/img]

一、xml:
<?xml version="1.0" encoding="utf-8"?>
<students>
	<student id="1">
		<name>tom</name>
		<age>20</age>
	</student>
	
	<student id="2">
		<name>kite</name>
		<age>21</age>
	</student>
	
	<student id="3">
		<name>rose</name>
		<age>22</age>
	</student>

</students>


二、Student类:
package com.amaker.pullparser;

public class Student {
	
	private int id;
	private int age;
	private String name;
	/**
	 * 无参构造方法
	 */
	public Student() {
		super();
	}
	/**
	 * 带参构造方法
	 */
	public Student(int id, int age, String name) {
		super();
		this.id = id;
		this.age = age;
		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;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

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


三、MainActivity类:
package com.amaker.pullparser;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

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

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        List<Student> list = readXML();
        for(Student s:list){
        	Log.i("test", s.toString());
        }
        
    }
    
    List<Student> readXML(){
    	InputStream in = this.getClass().getClassLoader().getResourceAsStream("stu.xml");
    	XmlPullParser parser = Xml.newPullParser();
    	List<Student> studentList = new ArrayList<Student>();
    	Student currentStudent = null;
    	try {
			parser.setInput(in, "utf-8");
			int eventType = parser.getEventType();
			while(eventType!=XmlPullParser.END_DOCUMENT){
				switch (eventType) {
				
				case XmlPullParser.START_TAG:
					String tagName = parser.getName();
					if(tagName!=null&&tagName.equals("student")){
						currentStudent = new Student();
						int id = Integer.parseInt(parser.getAttributeValue(null,"id"));
						currentStudent.setId(id);
					}
					
					if(tagName!=null&&tagName.equals("name")){
						String name;
						try {
							name = parser.nextText();
							currentStudent.setName(name);
						} catch (IOException e) {
							e.printStackTrace();
						}
						
					}
					
					if(tagName!=null&&tagName.equals("age")){
						String age;
							try {
								age = parser.nextText();
								currentStudent.setAge(Integer.parseInt(age));
							} catch (IOException e) {
								e.printStackTrace();
							}
						
					}
					break;
					
				case XmlPullParser.END_TAG:
					if(parser.getName().equals("student")){
						studentList.add(currentStudent);
					}
					break;
				default:
					break;
				}
				eventType = parser.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return studentList;
    }
}

你可能感兴趣的:(xml,xmlpullparser)