当前位置: 代码迷 >> 综合 >> 解析Google天气的xml文件
  详细解决方案

解析Google天气的xml文件

热度:36   发布时间:2024-01-21 17:25:17.0

<?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:03package 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:38package 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;}@Overridepublic void startDocument() throws SAXException {// 开始文件}@Overridepublic 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;}@Overridepublic void characters(char[] ch, int start, int length)throws SAXException {// 遇到文字(标签之间)}@Overridepublic 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;}@Overridepublic void startDocument() throws SAXException {// 开始文件weatherBeans = new ArrayList<WeatherBean>();}@Overridepublic 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;}@Overridepublic void characters(char[] ch, int start, int length)throws SAXException {// 遇到文字(标签之间)}@Overridepublic void endElement(String uri, String localName, String qName)throws SAXException {// 结束节点if ("forecast_conditions".equals(localName)) {weatherBeans.add(weatherBean);weatherBean = null;}tag = null;}}
}

SAX解析







  相关解决方案