1. 程式人生 > >Android 中使用Pull解析XML檔案

Android 中使用Pull解析XML檔案

解析XML檔案是非常常用的功能,在Android客戶端中,經常與伺服器通訊都需要xml檔案的支援,我們這裡介紹一個

簡單的xml檔案的解析,就是使用android中的pull方法進行解析。在java中,有dom解析和sax解析,這個pull解析有些類

似於sax解析,他也是一行一行的讀取然後解析內容的方法.

首先看一下這個簡單的xml檔案

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

<infos>

	<city id="1">
		<temp>-1℃/5℃</temp>
		<weather>多雲</weather>
		<wind>南風3-4級</wind>
		<name>上海</name>
		<pm>200</pm>
	</city>

<city id="2">
		<temp>-1℃/5℃</temp>
		<weather>多雲</weather>
		<wind>南風3-4級</wind>
		<name>北京7-8</name>
		<pm>800</pm>
	</city>


<city id="3">
		<temp>-7℃/5℃</temp>
		<weather>多雲</weather>
		<wind>南風3-4級</wind>
		<name>哈爾濱</name>
		<pm>100</pm>
	</city>

</infos>


然後我們直接解析這個xml檔案,在textview中顯示一下

這裡是程式碼,首先是業務Bean

package com.bird.weather;

public class WeatherBean {

	private int id;
	private String name;
	private String wind;
	private String weather;
	private String temp;
	private String pm;
	
	

	@Override
	public String toString() {
		return "WeatherBean [id=" + id + ", name=" + name + ", wind=" + wind
				+ ", weather=" + weather + ", temp=" + temp + ", pm=" + pm
				+ "]";
	}

	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 String getWind() {
		return wind;
	}

	public void setWind(String wind) {
		this.wind = wind;
	}

	public String getWeather() {
		return weather;
	}

	public void setWeather(String weather) {
		this.weather = weather;
	}

	public String getTemp() {
		return temp;
	}

	public void setTemp(String temp) {
		this.temp = temp;
	}

	public String getPm() {
		return pm;
	}

	public void setPm(String pm) {
		this.pm = pm;
	}
}

然後是解析xml檔案的主要程式碼
package com.bird.weather;

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

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

public class ParseXml {

	public static List<WeatherBean> parse(InputStream is) {
		List<WeatherBean> list = null;
		WeatherBean bean = null;
		try {
			XmlPullParser parser = Xml.newPullParser();
			// 初始化解析器
			parser.setInput(is, "utf-8");

			int type = parser.next();
			while (type != XmlPullParser.END_DOCUMENT) {
				switch (type) {
				case XmlPullParser.START_TAG:
					if ("infos".equals(parser.getName())) {
						list = new ArrayList<WeatherBean>();
					} else if ("city".equals(parser.getName())) {
						bean = new WeatherBean();
						bean.setId(Integer.valueOf(parser.getAttributeValue(0)));
					} else if ("temp".equals(parser.getName())) {
						String temp = parser.nextText();
						bean.setTemp(temp);
					} else if ("weather".equals(parser.getName())) {
						String weather = parser.nextText();
						bean.setWeather(weather);
					} else if ("wind".equals(parser.getName())) {
						String wind = parser.nextText();
						bean.setWind(wind);
					} else if ("name".equals(parser.getName())) {
						String name = parser.nextText();
						bean.setName(name);
					} else if ("pm".equals(parser.getName())) {
						String pm = parser.nextText();
						bean.setPm(pm);
					}
					break;

				case XmlPullParser.END_TAG:
					if ("city".equals(parser.getName())) {
						// 一個城市的資訊處理完畢
						list.add(bean);
						bean = null;
					}
					break;
				}

				type = parser.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}

最後在mainactivity中使用這個程式碼,使用類載入器完成這個簡單的功能
package com.bird.weather;

import java.util.List;

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

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        TextView tv =  (TextView) findViewById(R.id.tv);
        List<WeatherBean> list = ParseXml.parse(MainActivity.class.getClassLoader().getResourceAsStream("test.xml"));
        StringBuffer sb = new StringBuffer();
        for(WeatherBean bean : list){
        	String str = bean.toString();
        	sb.append(str);
        	sb.append("\n");
        }
        
        tv.setText(sb.toString());
    }

    
}


這樣看來,解析xml檔案還是非常簡單的