SAX解析百度车联网天气查询得到的xml数据

前端之家收集整理的这篇文章主要介绍了SAX解析百度车联网天气查询得到的xml数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

由于中国天气网的天气API不更新数据,我就使用百度车联网天气查询的API,需要得到的天气没怎么详细,但还是先用着,上效果


关于定位部分可以查看:Android基于百度定位SDK(LBS)实现定位功能

车联网的天气查询可以得到json或xml数据,这里只讲如何解析xml数据;更详细信息可以查看:车联网天气查询

从网页上读取到的数据是

<CityWeatherResponse>
<status>success</status>
<date>2014-04-14</date>
	<results> 
		<currentCity>深圳</currentCity>
		<weather_data>
			<date>周一(今天,实时:23℃)</date>
			<dayPictureUrl>http://api.map.baidu.com/images/weather/day/yin.png</dayPictureUrl>
			<nightPictureUrl>http://api.map.baidu.com/images/weather/night/yin.png</nightPictureUrl>
			<weather>阴</weather>
			<wind>微风</wind>
			<temperature>26 ~ 21℃</temperature>
			<date>周二</date>
			<dayPictureUrl>http://api.map.baidu.com/images/weather/day/duoyun.png</dayPictureUrl>
			<nightPictureUrl>http://api.map.baidu.com/images/weather/night/duoyun.png</nightPictureUrl>
			<weather>多云</weather>
			<wind>微风</wind>
			<temperature>27 ~ 20℃</temperature>
			<date>周三</date>
			<dayPictureUrl>http://api.map.baidu.com/images/weather/day/duoyun.png</dayPictureUrl>
			<nightPictureUrl>http://api.map.baidu.com/images/weather/night/duoyun.png</nightPictureUrl>
			<weather>多云</weather>
			<wind>微风</wind>
			<temperature>28 ~ 20℃</temperature>
			<date>周四</date>
			<dayPictureUrl>http://api.map.baidu.com/images/weather/day/duoyun.png</dayPictureUrl>
			<nightPictureUrl>http://api.map.baidu.com/images/weather/night/duoyun.png</nightPictureUrl>
			<weather>多云</weather>
			<wind>微风</wind>
			<temperature>29 ~ 22℃</temperature>
		</weather_data>
	</results>
</CityWeatherResponse>

这个APP只要就是读取指定的xml,并解析让用户看起来更直观

程序目录结构


获取xml数据WeatherSource.java

package com.dzt.weather;

import android.os.AsyncTask;

import com.dzt.weather.constant.GlobalConstants;
import com.dzt.weather.utils.NetworkUtils;

public class WeatherSource {

	private boolean mIsFinish = false;
	private String mResult;
	private OnFinishListener mFinishListener = null;
	private String mCity;

	public WeatherSource(String city) {
		mCity = city;
		new GetSource().execute();
	}

	public boolean isFinish() {
		return mIsFinish;
	}

	public String getResult() {
		return mResult;
	}

	class GetSource extends AsyncTask<String,Integer,String> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			GlobalConstants.PrintLog_D("[GetSource->doInBackground]");
			mIsFinish = false;
			try { // http://www.weather.com.cn/data/sk/101280601.html 不再更新天气
				String path = "http://api.map.baidu.com/telematics/v3/weather?location="
						+ mCity
						+ "&output=xml&ak=640f3985a6437dad8135dae98d775a09";
				return NetworkUtils.HttpGet(path);
			} catch (Exception e) {
				System.out.println("doInBackground--Exception");
				e.printStackTrace();
				return null;
			} // http://61.4.185.48:81/g/
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			mResult = result;
			mIsFinish = true;
			if (mFinishListener != null) {
				mFinishListener.OnFinish(result);
			}
			GlobalConstants.PrintLog_D("[GetSource->onPostExecute]" + result);
		}
	}

	public void setOnFinishListener(OnFinishListener listener) {
		mFinishListener = listener;
	}

	public interface OnFinishListener {
		public void OnFinish(String result);
	}
}
得到数据后通过SAX解析xml数据WeatherHandler.java
package com.dzt.weather;

import java.util.ArrayList;

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

/**
 * 使用SAX解析百度车联网天气查询得到的xml文件。 http://developer.baidu.com/map/carapi-7.htm
 * 
 * @author dzt 2014.04.14
 * 
 */
public class WeatherHandler extends DefaultHandler {
	String status,date,currentCity;
	String tagName;
	WeatherInfo weatherInfo = null;
	ArrayList<WeatherInfo> weatherInfos;
	int flag = 0;

	public ArrayList<WeatherInfo> getWeathers() {
		return weatherInfos;
	}

	public String getDate() {
		return date;
	}

	public String getCity() {
		return currentCity;
	}

	/**
	 * 开始解析xml文件
	 */
	public void startDocument() throws SAXException {
		weatherInfos = new ArrayList<WeatherInfo>();
		System.out.println("````````begin````````");
	}

	/**
	 * 解析xml文件结束
	 */
	public void endDocument() throws SAXException {
		System.out.println("````````end````````");
	}

	/**
	 * 开始解析标签
	 */
	public void startElement(String namespaceURI,String localName,String qName,Attributes attr) throws SAXException {
		tagName = localName;
		if (localName.equals("CityWeatherResponse")) {
			System.out.println("start-----CityWeatherResponse");
			// 获取标签的全部属性
			for (int i = 0; i < attr.getLength(); i++) {
				System.out.println("startElement " + attr.getLocalName(i) + "="
						+ attr.getValue(i));
			}
		} else if (localName.equals("date")) {
			flag++;
			if (flag > 1) {
				weatherInfo = new WeatherInfo();
			}
		} else if (localName.equals("results")) {
			System.out.println("start-----results");
		} else if (localName.equals("weather_data")) {
			System.out.println("start-----weather_data");
		}
	}

	/**
	 * 读取到内容
	 */
	public void characters(char[] ch,int start,int length)
			throws SAXException {
		// System.out.println("characters---tagName = " + tagName);
		if (tagName != null) {
			String data = new String(ch,start,length);
			if (tagName.equals("status"))
				status = data;
			else if (tagName.equals("currentCity"))
				currentCity = data;
			else if (tagName.equals("date")) {
				if (flag == 1) {
					date = data;
				} else {
					weatherInfo.setDate(data);
				}
			} else if (tagName.equals("dayPictureUrl")) {
				weatherInfo.setDayPictureUrl(data);
			} else if (tagName.equals("nightPictureUrl")) {
				weatherInfo.setNightPictureUrl(data);
			} else if (tagName.equals("weather")) {
				weatherInfo.setWeather(data);
			} else if (tagName.equals("wind")) {
				weatherInfo.setWind(data);
			} else if (tagName.equals("temperature")) {
				weatherInfo.setTemperature(data);
			}
		}
	}

	/**
	 * 解析标签结束
	 */
	public void endElement(String namespaceURI,String qName)
			throws SAXException {
		// 在workr标签解析完之后,会打印出所有得到的数据
		tagName = "";
		if (localName.equals("CityWeatherResponse")) {
			System.out.println("end-----CityWeatherResponse");
			flag = 0;
			// printout();
		} else if (localName.equals("results")) {
			System.out.println("end-----results");
		} else if (localName.equals("weather_data")) {
			System.out.println("end-----weather_data");
		} else if (localName.equals("temperature")) {
			weatherInfos.add(weatherInfo);
		}
	}

	// 打开一个标签内容
	public void printout() {
		System.out.println("status: " + status);
		System.out.println("date: " + date);
		System.out.println("currentCity: " + currentCity);
		for (int i = 0; i < weatherInfos.size(); i++) {
			System.out.println(weatherInfos.get(i).toString());
		}
		System.out.println();
	}

}
由于百度车联网天气查询有两个date标签,所以在这里我用flag变量来控制。

天气实体类WeatherInfo.java

package com.dzt.weather;

/**
 * 天气信息实体类
 * 
 * @author dzt 2014.04.12
 * 
 */
public class WeatherInfo {
	private String date; // 日期
	private String dayPictureUrl; // 白天天气图片
	private String nightPictureUrl; // 晚上天气图片
	private String weather;
	private String wind;
	private String temperature;

	public WeatherInfo() {
	}

	public WeatherInfo(String date,String dayPictureUrl,String nightPictureUrl,String weather,String wind,String temperature) {
		this.date = date;
		this.dayPictureUrl = dayPictureUrl;
		this.nightPictureUrl = nightPictureUrl;
		this.weather = weather;
		this.wind = wind;
		this.temperature = temperature;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getDayPictureUrl() {
		return dayPictureUrl;
	}

	public void setDayPictureUrl(String dayPictureUrl) {
		this.dayPictureUrl = dayPictureUrl;
	}

	public String getNightPictureUrl() {
		return nightPictureUrl;
	}

	public void setNightPictureUrl(String nightPictureUrl) {
		this.nightPictureUrl = nightPictureUrl;
	}

	public String getWeather() {
		return weather;
	}

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

	public String getWind() {
		return wind;
	}

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

	public String getTemperature() {
		return temperature;
	}

	public void setTemperature(String temperature) {
		this.temperature = temperature;
	}

	@Override
	public String toString() {
		return "WeatherInfo [date=" + date + ",dayPictureUrl=" + dayPictureUrl
				+ ",nightPictureUrl=" + nightPictureUrl + ",weather="
				+ weather + ",wind=" + wind + ",temperature=" + temperature
				+ "]";
	}
}

更多的代码我就不贴出来了,有需要的可以下载:http://download.csdn.net/detail/deng0zhaotai/7192795

声明下必须要有网络才能定位和获取天气信息。

今天(2014.05.05)在网上找到一个更详细的天气信息

http://wthrcdn.etouch.cn/weather_mini?city=深圳
通过城市名字获得天气数据,json数据
http://wthrcdn.etouch.cn/weather_mini?citykey=101280601
通过城市id获得天气数据,json数据

http://wthrcdn.etouch.cn/WeatherApi?citykey=101280601 通过城市id获得天气数据,xml文件数据,当错误时会有<error>节点 http://wthrcdn.etouch.cn/WeatherApi?city=深圳 通过城市名字获得天气数据,xml文件数据

原文链接:https://www.f2er.com/xml/298865.html

猜你在找的XML相关文章