http://flash.weather.com.cn/wmaps/xml/wuhan.xml 使用PULL把数据解析出来并放到集合里面
Weather类:
/**** 天气类* 这个类感觉定义的有点乱* 没有抽象出一些类 为了解析方便* @author Mixm* @date 2015年10月9日 下午8:21:53*/public class Weather {private String cityX;private String cityY;private String cityname;private String centername;private String fontColor;private String pyName;private String state1;private String state2;private String stateDetailed;private String tem1;private String tem2;private String temNow;private String windState;private String windDir;private String windPower;private String humidity;private String time;private String url;@Overridepublic String toString() {return "Weather [cityX=" + cityX + ", cityY=" + cityY + ", cityname="+ cityname + ", centename=" + centername + ", fontColor="+ fontColor + ", pyName=" + pyName + ", state1=" + state1+ ", state2=" + state2 + ", stateDetailed=" + stateDetailed+ ", tem1=" + tem1 + ", tem2=" + tem2 + ", temNow=" + temNow+ ", windState=" + windState + ", windDir=" + windDir+ ", windPower=" + windPower + ", humidity=" + humidity+ ", time=" + time + ", url=" + url + "]";}public void setCityX(String cityX) {this.cityX = cityX;}public void setCityY(String cityY) {this.cityY = cityY;}public void setCityname(String cityname) {this.cityname = cityname;}public void setCentername(String centename) {this.centername = centename;}public void setFontColor(String fontColor) {this.fontColor = fontColor;}public void setPyName(String pyName) {this.pyName = pyName;}public void setState1(String state1) {this.state1 = state1;}public void setState2(String state2) {this.state2 = state2;}public void setStateDetailed(String stateDetailed) {this.stateDetailed = stateDetailed;}public void setTem1(String tem1) {this.tem1 = tem1;}public void setTem2(String tem2) {this.tem2 = tem2;}public void setTemNow(String temNow) {this.temNow = temNow;}public void setWindState(String windState) {this.windState = windState;}public void setWindDir(String windDir) {this.windDir = windDir;}public void setWindPower(String windPower) {this.windPower = windPower;}public void setHumidity(String humidity) {this.humidity = humidity;}public void setTime(String time) {this.time = time;}public void setUrl(String url) {this.url = url;}}
解析类:
import java.io.ByteArrayInputStream;import java.io.InputStreamReader;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserFactory;/*** http://flash.weather.com.cn/wmaps/xml/wuhan.xml 使用PULL把数据解析出来并放到集合里面** @author Mixm* @date 2015年10月9日 下午8:07:01*/public class WeatherParse {public static void main(String[] args) {String url = "http://flash.weather.com.cn/wmaps/xml/wuhan.xml";byte[] b = getXML(url);List<Weather> lists = getWeather(b);for (Weather weather : lists) {System.out.println(weather);}}// 解析并添加到list集合中public static List<Weather> getWeather(byte[] b) {List<Weather> lists = null;Weather weather = null;try {XmlPullParserFactory factory = XmlPullParserFactory.newInstance();XmlPullParser parser = factory.newPullParser();//这儿记得转换parser.setInput(new InputStreamReader(new ByteArrayInputStream(b)));int type = parser.getEventType();while (type != 1) {switch (type) {case 0:lists = new ArrayList<Weather>();case 2:String tag = parser.getName();if ("city".equals(tag)) {weather = new Weather();for (int i = 0; i < parser.getAttributeCount(); i++) {// 对于每一个属性,得到获得属性名组装成方法 然后调用方法并传入相应的值getSetMethod(weather.getClass().getName(),parser.getAttributeName(i)).invoke(weather,parser.getAttributeValue(i));}}break;case 3:String tag_ = parser.getName();if ("city".equals(tag_)) {lists.add(weather);weather = null;}break;default:break;}type = parser.next();}} catch (Exception e) {e.printStackTrace();return lists;}return lists;}/**得到xml文件的byte类型数组*/public static byte[] getXML(String url) {HttpClient client = new DefaultHttpClient();HttpGet get = new HttpGet(url);HttpResponse response;try {response = client.execute(get);if (response.getStatusLine().getStatusCode() == 200) {return EntityUtils.toByteArray(response.getEntity());} else {System.out.println("相应错误");}} catch (Exception e) {e.printStackTrace();}return null;}/*** 利用反射获得set***方法*/public static Method getSetMethod(String className, String fieldName) {try {Class cl = Class.forName(className);StringBuffer sb = new StringBuffer();sb.append("set");sb.append(fieldName.substring(0, 1).toUpperCase());sb.append(fieldName.substring(1));Method method = cl.getMethod(sb.toString(), String.class);return method;} catch (Exception e) {e.printStackTrace();}return null;}}
本文介绍了一种使用Pull解析器解析XML格式天气数据的方法,并将解析后的数据存入Weather类实例组成的列表中。该方法适用于从特定URL获取XML数据并进行解析。
1215

被折叠的 条评论
为什么被折叠?



