加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

采用pull解析xml文件

发布时间:2020-12-16 05:53:00 所属栏目:百科 来源:网络整理
导读:把weather.xml文件放在src的根目录下 ?xml version="1.0" encoding="utf-8"?infos city id="1" temp20℃/30℃/temp weather5月20日 多云转阴/weatherwind南风3-4级/windname上海/namepm200/pm/city city id="2" temp26℃/35℃/temp weather5月20日 多云转阴/

把weather.xml文件放在src的根目录下

<?xml version="1.0" encoding="utf-8"?>
<infos>
    <city id="1">
   	    <temp>20℃/30℃</temp>
   	    <weather>5月20日 多云转阴</weather>
		<wind>南风3-4级</wind>
		<name>上海</name>
		<pm>200</pm>
	</city>
    <city id="2">
   	    <temp>26℃/35℃</temp>
   	    <weather>5月20日 多云转阴</weather>
		<wind>南风1-2级</wind>
		<name>北京</name>
		<pm>100</pm>
	</city>
    <city id="3">
   	    <temp>15℃/25℃</temp>
   	    <weather>5月20日 多云转阴</weather>
		<wind>南风6-7级</wind>
		<name>哈尔滨</name>
		<pm>100</pm>
	</city>
</infos>
创建一个TextView
<TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
model层(getset对儿,toString方法)
private int id;
private String name;
private String wind;
private String temp;
private String weather;
private String pm;
Service层
package org.gentry.weather.service;

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

import org.gentry.weather.domain.WeatherInfo;
import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

public class WeatherService {
	public static List<WeatherInfo> getWeatherInfos(InputStream is)
			throws Exception {
		XmlPullParser parser = Xml.newPullParser(); // xml解析器
		// 初始化解析器
		parser.setInput(is,"utf-8");
		List<WeatherInfo> weatherInfos = null;
		WeatherInfo weatherInfo = null;
		int type = parser.getEventType(); // 得到当前的事件类型
		while (type != XmlPullParser.END_DOCUMENT) {
			switch (type) {
			case XmlPullParser.START_TAG:
				if ("infos".equals(parser.getName())) {
					// 解析到全局开始的标签
					weatherInfos = new ArrayList<WeatherInfo>();
				} else if ("city".equals(parser.getName())) {
					weatherInfo = new WeatherInfo();
					String idStr = parser.getAttributeValue(0);
					weatherInfo.setId(Integer.parseInt(idStr));
				} else if ("temp".equals(parser.getName())) {
					String temp = parser.nextText();
					weatherInfo.setTemp(temp);
				} else if ("weather".equals(parser.getName())) {
					String weather = parser.nextText();
					weatherInfo.setWeather(weather);
				} else if ("wind".equals(parser.getName())) {
					String wind = parser.nextText();
					weatherInfo.setWind(wind);
				} else if ("name".equals(parser.getName())) {
					String name = parser.nextText();
					weatherInfo.setName(name);
				} else if ("pm".equals(parser.getName())) {
					String pm = parser.nextText();
					weatherInfo.setPm(pm);
				}
				break;

			case XmlPullParser.END_TAG:
				if ("city".equals(parser.getName())) {
					// 一个城市的信息已经处理完毕了
					weatherInfos.add(weatherInfo);
					weatherInfo = null;
				}
				break;
			}
			type = parser.next();
		}
		return weatherInfos;
	}
}
MainActivity
package org.gentry.weather;


import java.util.List;

import org.gentry.weather.domain.WeatherInfo;
import org.gentry.weather.service.WeatherService;

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

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);

		try {
			List<WeatherInfo> infos = WeatherService.getWeatherInfos(MainActivity.class.getClassLoader()
					.getResourceAsStream("weather.xml"));
			StringBuffer sb = new StringBuffer();
			for (WeatherInfo info : infos) {
				String str = info.toString();
				sb.append(str);
				sb.append("n");
			}
			tv.setText(sb.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Toast.makeText(this,"解析信息失败",Toast.LENGTH_SHORT).show();
		}
	}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读