解析Google天气的xml文件

<?xml version="1.0" encoding="UTF-8"?>
<xml_api_reply version="1">
	<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
		<forecast_information>
			<city data="Qingdao, Shandong" />
			<postal_code data="Qingdao" />
			<latitude_e6 data="" />
			<longitude_e6 data="" />
			<forecast_date data="2012-04-17" />
			<current_date_time data="2012-04-18 02:00:00 +0000" />
			<unit_system data="SI" />
		</forecast_information>
		<current_conditions>
			<condition data="晴" />
			<temp_f data="52" />
			<temp_c data="11" />
			<humidity data="湿度: 33%" />
			<icon data="/ig/images/weather/sunny.gif" />
			<wind_condition data="风向: 南、风速:7 米/秒" />
		</current_conditions>
		<forecast_conditions>
			<day_of_week data="周二" />
			<low data="7" />
			<high data="19" />
			<icon data="/ig/images/weather/mostly_sunny.gif" />
			<condition data="以晴为主" />
		</forecast_conditions>
		<forecast_conditions>
			<day_of_week data="周三" />
			<low data="7" />
			<high data="16" />
			<icon data="/ig/images/weather/mostly_sunny.gif" />
			<condition data="晴间多云" />
		</forecast_conditions>
		<forecast_conditions>
			<day_of_week data="周四" />
			<low data="7" />
			<high data="17" />
			<icon data="/ig/images/weather/cn_fog.gif" />
			<condition data="雾" />
		</forecast_conditions>
		<forecast_conditions>
			<day_of_week data="周五" />
			<low data="8" />
			<high data="17" />
			<icon data="/ig/images/weather/cn_fog.gif" />
			<condition data="雾" />
		</forecast_conditions>
	</weather>
</xml_api_reply>

首先是Google天气的api.xml

// 2012-4-17下午07:09:03

package com.su.weatherxml;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.util.EntityUtils;
import org.xmlpull.v1.XmlPullParser;

import android.util.Log;
import android.util.Xml;
import android.widget.Toast;

public class PULLMode {
	private static final String TAG = "PULLMODE";

	public HashMap<String, String> getCurrentInfo(InputStream inStream)
			throws Throwable {
		Log.i(TAG, "1");
		HashMap<String, String> infoMap = new HashMap<String, String>();
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(inStream, "UTF-8");
		int eventType = parser.getEventType();// 产生第一个事件

		Log.i(TAG, eventType + "");
		while (eventType != XmlPullParser.END_DOCUMENT) {// 只要不是文档结束事件

			switch (eventType) {
			case XmlPullParser.START_DOCUMENT:

				break;

			case XmlPullParser.START_TAG:
				String name = parser.getName();// 获取解析器当前指向的元素的名称

				if ("city".equals(name)) {
					infoMap.put("city", parser.getAttributeValue(0));// 获取解析器当前指向元素的下一个文本节点的值
				}
				if ("wind_condition".equals(name)) {
					infoMap.put("wind_condition", parser.getAttributeValue(0));
				}
				break;
			case XmlPullParser.END_TAG:
				if ("xml_api_reply".equals(parser.getName())) {

					Iterator iter = infoMap.entrySet().iterator();
					while (iter.hasNext()) {
						Map.Entry entry = (Map.Entry) iter.next();
						String key = entry.getKey().toString();
						String val = entry.getValue().toString();
						Log.i(TAG, key + "    " + val);// 遍历hashmap方法输出信息
					}

					break;
				}
			}
			eventType = parser.next();// 每个节点都要进行判断
		}
		return infoMap;
	}

	public List<WeatherBean> getWeatherBeans(InputStream inStream)
			throws Throwable {
		List<WeatherBean> wheatherBeans = null;
		WeatherBean wheatherBean = null;
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(inStream, "UTF-8");

		int eventType = parser.getEventType();// 产生第一个事件
		while (eventType != XmlPullParser.END_DOCUMENT) {// 只要不是文档结束事件就进行循环
			switch (eventType) {
			case XmlPullParser.START_DOCUMENT:
				wheatherBeans = new ArrayList<WeatherBean>();
				break;

			case XmlPullParser.START_TAG:
				String name = parser.getName();// 获取解析器当前指向的元素的名称
				if ("forecast_conditions".equals(name))
					wheatherBean = new WeatherBean();

				if (wheatherBean != null) {// 只有当whetherBean被new后才执行这里 向里面添加数据
					if ("day_of_week".equals(name)) {
						wheatherBean.setDayofweek(parser.getAttributeValue(0));
						Log.i(TAG, parser.getAttributeValue(0));
						// parser还有很多其他的方法 比如nextText();
					}
					if ("low".equals(name)) {
						wheatherBean.setLow(new Integer(parser
								.getAttributeValue(0)));
						Log.i(TAG, parser.getAttributeValue(0));
					}
					if ("high".equals(name)) {
						wheatherBean.setHigh(new Integer(parser
								.getAttributeValue(0)));
						Log.i(TAG, parser.getAttributeValue(0));
					}
					if ("condition".equals(name)) {
						wheatherBean.setCondition(parser.getAttributeValue(0));
						Log.i(TAG, parser.getAttributeValue(0));
					}
				}
				break;

			case XmlPullParser.END_TAG:
				if ("forecast_conditions".equals(parser.getName())) {
					wheatherBeans.add(wheatherBean);
					wheatherBean = null;

				}
				break;
			}
			eventType = parser.next();
		}
		return wheatherBeans;
	}

}

PULL解析的方法;由于api.xml的前面几行不是很对称,所以把前面的信息存贮在了map中,后面的存储在list<WeatherBean>


// 2012-4-17下午08:41:38

package com.su.weatherxml;

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

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class SAXMode {
	public static final String TAG = "SAXmode";

	public HashMap<String, String> getCurrentInfo(InputStream inStream)
			throws Throwable {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser = factory.newSAXParser();
		mWeatherParser mweatherParser = new mWeatherParser();
		parser.parse(inStream, mweatherParser);
		inStream.close();
		return mweatherParser.getWeatherBeans();
	}

	private final class mWeatherParser extends DefaultHandler {
		
		private HashMap<String, String> infoMap = new HashMap<String, String>();;
		private String tag = null;
		public HashMap<String, String> getWeatherBeans() {
			return infoMap;
		}

		@Override
		public void startDocument() throws SAXException {// 开始文件

		}

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			
			if (infoMap != null) {
				if ("city".equals(localName)) {
					infoMap.put("city", attributes.getValue(0));
					Log.i(TAG, attributes.getValue(0));
				}
				if ("wind_condition".equals(localName)) {
					infoMap.put("wind_condition",
							attributes.getValue(0));
					Log.i(TAG, attributes.getValue(0));
				}
			}
			tag = localName;
		}

		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {// 遇到文字(标签之间)

		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {// 结束节点
			tag = null;
		}
	}

	public List<WeatherBean> getWeatherBeans(InputStream inStream)
			throws Throwable {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser = factory.newSAXParser();
		WeatherParser weatherParser = new WeatherParser();
		parser.parse(inStream, weatherParser);
		inStream.close();
		return weatherParser.getWeatherBeans();
	}

	private final class WeatherParser extends DefaultHandler {
		private List<WeatherBean> weatherBeans = null;
		private String tag = null;
		private WeatherBean weatherBean = null;

		public List<WeatherBean> getWeatherBeans() {
			return weatherBeans;
		}

		@Override
		public void startDocument() throws SAXException {// 开始文件
			weatherBeans = new ArrayList<WeatherBean>();
		}

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			if ("forecast_conditions".equals(localName)) {// 开始解析节点(标签)
				weatherBean = new WeatherBean();
			}
			if (weatherBean != null) {
				if ("day_of_week".equals(localName)) {
					weatherBean.setDayofweek(attributes.getValue(0));
					Log.i(TAG, attributes.getValue(0));
				}
				if ("low".equals(localName)) {
					weatherBean.setLow(new Integer(attributes.getValue(0)));
				}
				if ("high".equals(localName)) {
					weatherBean.setHigh(new Integer(attributes.getValue(0)));
				}
				if ("condition".equals(localName)) {
					weatherBean.setCondition(attributes.getValue(0));
				}
			}

			tag = localName;
		}

		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {// 遇到文字(标签之间)

		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {// 结束节点
			if ("forecast_conditions".equals(localName)) {
				weatherBeans.add(weatherBean);
				weatherBean = null;
			}
			tag = null;
		}
	}
}

SAX解析







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值