XmlPaseService.java 解析类
public class XmlPaseService {
public static List<Object> parseXml(InputStream is) throws Exception {
String text = "";
StringBuffer urll = new StringBuffer();
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"GBK"));
while((text = reader.readLine())!=null){
urll.append(text);
}
// System.out.println("urll:::::::::"+urll.toString());
parser.setInput(new StringReader(urll.toString()));
// XmlPullParser parser = Xml.newPullParser();
// parser.setInput(is, "GBK");
int type = parser.getEventType();
Weatherdto weatherdto = null;
CurrentWeatherdto currentWeatherdto = null;
List<Weatherdto> weatherdtoList = null;
while(type!=XmlPullParser.END_DOCUMENT){
switch(type){
case XmlPullParser.START_DOCUMENT:
weatherdtoList = new ArrayList<Weatherdto>();
break;
case XmlPullParser.START_TAG:
if("forecast_conditions".equals(parser.getName())){
weatherdto = new Weatherdto();
}else if(weatherdto!=null){
if("day_of_week".equals(parser.getName())){
weatherdto.setDay_of_week(parser.getAttributeValue(0));
}
if("low".equals(parser.getName())){
weatherdto.setLow_temperature(new Integer(parser.getAttributeValue(0)));
}
if("high".equals(parser.getName())){
weatherdto.setHigh_temperature(new Integer(parser.getAttributeValue(0)));
}
if("icon".equals(parser.getName())){
weatherdto.setIcon(parser.getAttributeValue(0));
}
if("condition".equals(parser.getName())){
weatherdto.setCondition(parser.getAttributeValue(0));
}
}
if("current_conditions".equals(parser.getName())){
currentWeatherdto = new CurrentWeatherdto();
}else if(currentWeatherdto!=null){
if("condition".equals(parser.getName())){
currentWeatherdto.setCondition(parser.getAttributeValue(0));
}
if("temp_f".equals(parser.getName())){
currentWeatherdto.setTemp_f(parser.getAttributeValue(0));
}
if("temp_c".equals(parser.getName())){
currentWeatherdto.setTemp_c(parser.getAttributeValue(0));
}
if("humidity".equals(parser.getName())){
currentWeatherdto.setHumidity(parser.getAttributeValue(0));
}
if("icon".equals(parser.getName())){
currentWeatherdto.setIcon(parser.getAttributeValue(0));
}
if("wind_condition".equals(parser.getName())){
currentWeatherdto.setWind_condition(parser.getAttributeValue(0));
}
}
break;
case XmlPullParser.END_TAG:
if("forecast_conditions".equals(parser.getName())){
if(weatherdto!=null&&weatherdtoList!=null){
weatherdtoList.add(weatherdto);
weatherdto = null;
}
}
break;
}
type = parser.next();
}
is.close();
List<Object> weather = new ArrayList<Object>();
weather.add(weatherdtoList);
weather.add(currentWeatherdto);
return weather;
}
}
CurrentWeatherdto.java 当前的天气实体
public class CurrentWeatherdto {
//实时天气情况
private String condition = "";
//华氏度
private String temp_f = "";
//摄氏度
private String temp_c = "";
//湿度
private String humidity ="";
//天气图标
private String icon = "";
//风情况
private String wind_condition = "";
public String getCondition() {
return condition;
}
public void setCondition(String condition) {
this.condition = condition;
}
public String getTemp_f() {
return temp_f;
}
public void setTemp_f(String temp_f) {
this.temp_f = temp_f;
}
public String getTemp_c() {
return temp_c;
}
public void setTemp_c(String temp_c) {
this.temp_c = temp_c;
}
public String getHumidity() {
return humidity;
}
public void setHumidity(String humidity) {
this.humidity = humidity;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getWind_condition() {
return wind_condition;
}
public void setWind_condition(String wind_condition) {
this.wind_condition = wind_condition;
}
}
Weatherdto.java 最近四天的天气情况实体
public class Weatherdto {
//星期几
private String day_of_week = "";
//最低气温
private int low_temperature = 0;
//最高气温
private int high_temperature = 0;
//图片名称
private String icon ="";
//基本信息
private String condition = "";
public String getDay_of_week() {
return day_of_week;
}
public void setDay_of_week(String day_of_week) {
this.day_of_week = day_of_week;
}
public int getLow_temperature() {
return low_temperature;
}
public void setLow_temperature(int low_temperature) {
this.low_temperature = low_temperature;
}
public int getHigh_temperature() {
return high_temperature;
}
public void setHigh_temperature(int high_temperature) {
this.high_temperature = high_temperature;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getCondition() {
return condition;
}
public void setCondition(String condition) {
this.condition = condition;
}
}