Android 用Pull解析XML方法解析谷歌天气

简介:

解析类:

package com.lzx.weather;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

public class WeatherModel {

	public static WeatherCurrentCondition curCondition = null;
	public static List<WeatherForecastCondition> forecastList = null;
	URL mUrl;
	public WeatherModel(URL url){
		this.mUrl = url;
		initData(mUrl);
	}
	
	public void initData(URL url){
		try {
			InputStream is = null;
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			is = conn.getInputStream();
			if(is != null){
				XmlPullParserFactory factory = XmlPullParserFactory.newInstance();    
				XmlPullParser parser = factory.newPullParser(); //      XmlPullParser parser = Xml.newPullParser();用这个解析不出来,值得注意

				parser.setInput(is, "GBK");
				WeatherForecastCondition forecastCondition = null;
				int eventType = parser.getEventType();
				Log.d("lzx", "eventType=" + eventType);
				while(eventType != XmlPullParser.END_DOCUMENT){
					switch (eventType) { 
					case XmlPullParser.START_DOCUMENT:
						Log.d("lzx", "START_DOCUMENT=" + parser.getName());
						forecastList = new ArrayList<WeatherForecastCondition>();
						break;
					case XmlPullParser.START_TAG:
						String name = parser.getName(); 
//						if("weather".equals(name)){
//							Log.d("lzx", "START_TAG=" + name);
//						}
						
						if("forecast_conditions".equals(name)){
							Log.d("lzx", "START_TAG=" + name);
							forecastCondition = new WeatherForecastCondition();
						}else if(forecastCondition != null){
							if("day_of_week".equals(name)){
								forecastCondition.day_of_week = parser.getAttributeValue(0);
							}
							if("low".equals(name)){
								forecastCondition.low = parser.getAttributeValue(0);
							}
							if("high".equals(name)){
								forecastCondition.high = parser.getAttributeValue(0);
							}
							if("icon".equals(name)){
								forecastCondition.icon = parser.getAttributeValue(0);
							}
							if("condition".equals(name)){
								forecastCondition.condition = parser.getAttributeValue(0);
							}
						}
						
						if("current_conditions".equals(name)){
							Log.d("lzx", "START_TAG=" + name);
							curCondition = new WeatherCurrentCondition();
						}else if(curCondition != null){
							if("condition".equals(name)){
								Log.d("lzx", "condition=" + parser.getAttributeValue(0));
								curCondition.condition = parser.getAttributeValue(0);
							}
							if("temp_f".equals(name)){
								curCondition.temp_fahrenheit = parser.getAttributeValue(0);
							}
							if("temp_c".equals(name)){
								curCondition.temp_celcius = parser.getAttributeValue(0);
							}
							if("humidity".equals(name)){
								curCondition.humidity = parser.getAttributeValue(0);
							}
							if("icon".equals(name)){
								curCondition.icon = parser.getAttributeValue(0);
							}
							if("wind_condition".equals(name)){
								curCondition.wind_condition = parser.getAttributeValue(0);
							} 
						}
						
						
						break; 
					
					case XmlPullParser.END_TAG:
						if("forecast_conditions".equals(parser.getName())){
							Log.d("lzx", "END_TAG=" + parser.getName());
							if(forecastList != null && forecastCondition != null){
								forecastList.add(forecastCondition);
								Log.d("lzx", "forecastList size=" + forecastList.size());
							}
							forecastCondition = null;
							
						}
						break;
					}
					eventType = parser.next();
				}
				Log.d("lzx", "curCondition--" + curCondition);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
}

WeatherCurrentCondition.java

package com.lzx.weather;

public class WeatherCurrentCondition {

	public String	condition;			// 多云
	public String	temp_celcius;		// 摄氏温度
	public String	temp_fahrenheit;	// 华氏温度
	public String	humidity;			// 湿度:58%
	public String	wind_condition;		// 风向...
	public String	icon;				// 图标
	
	//得到一个封装打包的字符串,包括除icno外的所有东西
	public String toString()
	{
		StringBuilder sb = new StringBuilder();
		sb.append("实时天气: ").append(temp_celcius).append(" °C");
		sb.append(" ").append(temp_fahrenheit).append(" F");
		sb.append(" ").append(condition);
		sb.append(" ").append(humidity);
		sb.append(" ").append(wind_condition);
		return sb.toString();
	}
}

WeatherForecastCondition.java

package com.lzx.weather;

public class WeatherForecastCondition {

	public String day_of_week;		//星期
	public String low;				//最低温度
	public String high;			//最高温度
	public String icon;			//图标
	public String condition;		//提示
	
	public String toString()
	{
		StringBuilder sb = new StringBuilder();

		sb.append(" ").append(day_of_week);
		sb.append(" : ").append(high);
		sb.append("/").append(low).append(" °C");
		sb.append(" ").append(condition);
		return sb.toString();
	}
}


相关文章
|
17天前
|
XML JavaScript 前端开发
xml文件使用及解析
xml文件使用及解析
|
23天前
|
XML Java Android开发
Android实现自定义进度条(源码+解析)
Android实现自定义进度条(源码+解析)
51 1
|
1月前
|
开发工具 git 开发者
|
1月前
|
开发工具 git 开发者
Git Pull vs. Git Fetch:深度解析
【2月更文挑战第29天】
135 0
Git Pull vs. Git Fetch:深度解析
|
4天前
|
XML Java 数据库连接
Javaweb之Mybatis的XML配置文件的详细解析
Javaweb之Mybatis的XML配置文件的详细解析
13 0
|
7天前
|
XML C# 数据格式
C# 解析XML文件
C# 解析XML文件
15 1
|
1月前
|
XML Java 数据格式
使用java解析XML文件的步骤
使用java解析XML文件的步骤
10 0
|
1月前
|
XML 存储 JavaScript
深入学习 XML 解析器及 DOM 操作技术
所有主要的浏览器都内置了一个XML解析器,用于访问和操作XML XML 解析器 在访问XML文档之前,必须将其加载到XML DOM对象中 所有现代浏览器都有一个内置的XML解析器,可以将文本转换为XML DOM对象
72 0
|
4天前
|
XML 数据格式
小米备份descript.xml文件
小米备份descript.xml文件
11 0
|
15天前
|
XML Java 数据库连接
mybatis中在xml文件中通用查询结果列如何使用
mybatis中在xml文件中通用查询结果列如何使用
19 0

推荐镜像

更多