pull解析xml

在工程中新建解析xml测试文件

dashu.xml

<?xml version="1.0" encoding="UTF-8"?>

<user-list>
	<user id="1">
		<name>张三</name>
		<age>22</age>
	</user>
	
	<user id="2">
		<name>李四</name>
		<age>28</age>
	</user>

	<user id="3">
		<name>王五</name>
		<age>32</age>
	</user>

</user-list>

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>

items.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text=""
        android:textSize="18sp" 
        android:textColor="@android:color/holo_blue_dark"/>


    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text=""
        android:textSize="18sp" 
        android:textColor="@android:color/holo_blue_dark"/>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text=""
        android:textSize="18sp"
        android:textColor="@android:color/holo_blue_dark" />

</LinearLayout>

PullXmlTools解析工具类

package com.example.dashu_xml;

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.R.integer;

/**
 * pull解析xml
 * */
public class PullXmlTools {
	public PullXmlTools() {
	}

	/**
	 * inputStream通常为服务器端获取的io流 encode编码格式
	 * 
	 * @throws XmlPullParserException
	 * @throws IOException
	 * @throws NumberFormatException
	 * */
	public static List<User> parseXML(InputStream inputStream, String encode)
			throws XmlPullParserException, NumberFormatException, IOException {
		List<User> list = null;
		User user = null;// 装载每一个节点的内容
		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		// 获得xml解析类引用
		XmlPullParser pullParser = factory.newPullParser();
		pullParser.setInput(inputStream, encode);
		// 获得事件类型
		int eventType = pullParser.getEventType();
		// 判定是否解析到文档结束
		while (eventType != XmlPullParser.END_DOCUMENT) {
			switch (eventType) {
			case XmlPullParser.START_DOCUMENT:// 开始文档
				list = new ArrayList<User>();
				break;
			case XmlPullParser.START_TAG:// 开始节点
				if ("user".equals(pullParser.getName())) {
					user = new User();
					int id = Integer.parseInt(pullParser.getAttributeValue(
							null, "id").toString());
					user.setId(id);
				}
				if (user != null) {
					if ("name".equals(pullParser.getName())) {
						String name = pullParser.nextText().toString();
						user.setName(name);
					}
					if ("age".equals(pullParser.getName())) {
						int age = Integer.parseInt(pullParser.nextText()
								.toString());// 取出id
						user.setAge(age);
					}
				}
				break;
			case XmlPullParser.END_TAG:// 结束节点时候
				if ("user".equals(pullParser.getName())) {
					list.add(user);
					user = null;
				}
				break;
			default:
				break;
			}
			eventType = pullParser.next();// 下一个解析标识
		}
		return list;
	}
}



User实体类

package com.example.dashu_xml;

public class User {
	private int id;
	private int age;
	private String name;

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

	public User() {
	}

	public User(int id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = 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;
	}
}

MyAdapter数据适配器

package com.example.dashu_xml;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {
	private List<User> list;
	private Context context;

	public MyAdapter(List<User> list, Context context) {
		this.list = list;
		this.context = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		if (arg1 == null) {
			LayoutInflater intInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			arg1 = intInflater.inflate(R.layout.items, null);
		}
		TextView textView1 = (TextView) arg1.findViewById(R.id.textView1);
		TextView textView2 = (TextView) arg1.findViewById(R.id.textView2);
		TextView textView3 = (TextView) arg1.findViewById(R.id.textView3);
		textView1.setText(list.get(arg0).getId() + "");
		textView2.setText(list.get(arg0).getName());
		textView3.setText(list.get(arg0).getAge() + "");
		return arg1;
	}

}

MainActivity

package com.example.dashu_xml;

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

import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.os.Build;

public class MainActivity extends Activity {
	List<User> list = null;
	InputStream inputStream = null;
	ListView listView;
	MyAdapter myAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		inputStream = getResources().openRawResource(R.raw.dashu);
		try {
			list = PullXmlTools.parseXML(inputStream, "UTF-8");
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		listView = (ListView) this.findViewById(R.id.listview);
		myAdapter=new MyAdapter(list, MainActivity.this);
		listView.setAdapter(myAdapter);
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值