关闭

pull解析xml

408人阅读 评论(0) 收藏 举报

不解释,直接上代码
<?xml version="1.0" encoding="utf-8"?>
<students>
	<student id="1">
		<name>张三</name>
		<age>18</age>
	</student>
	<student id="2">
		<name>李四</name>
		<age>33</age>
	</student>
	<student id="3">
		<name>王五</name>
		<age>12</age>
	</student>
</students>
---------------------

package com.u.v;

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

}
---------------------------

package com.u.v;

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 org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ABCpullActivity extends Activity {
	private TextView tv;
	private List<Student> students;
	private Student student;
	private StringBuffer sb = new StringBuffer();;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.tv);
        
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("s.xml");
        try {
			for(Student s:getlist(in)){
				sb.append(s.toString()+"\n");
			}
			tv.setText(sb.toString());
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
    }
	
	public List<Student> getlist(InputStream in) throws XmlPullParserException, IOException{
		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		XmlPullParser parser = factory.newPullParser();
		
		parser.setInput(in, "UTF-8");
		
		int event = parser.getEventType();
		while(XmlPullParser.END_DOCUMENT != event){
			String nodeName = parser.getName();
			
			switch (event) {
			case XmlPullParser.START_DOCUMENT:
				students = new ArrayList<Student>();
				break;
			case XmlPullParser.START_TAG:
				if(nodeName.equals("student")){
					student = new Student();
					student.setId(parser.getAttributeValue(0));
				}
				if(nodeName.equals("name")){
					student.setName(parser.nextText());
				}
				if(nodeName.equals("age")){
					student.setAge(parser.nextText());
				}
				break;
			case XmlPullParser.END_TAG:
				if(nodeName.equals("student") && student!=null){
					students.add(student);
				}
				break;
			default:
				break;
			}
			event = parser.next();
		}
		
		
		return students;
	}
	
}

















0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:303187次
    • 积分:4884
    • 等级:
    • 排名:第7174名
    • 原创:51篇
    • 转载:413篇
    • 译文:0篇
    • 评论:37条
    文章分类
    最新评论