1:weather.xml
<?xml version="1.0" encoding="utf-8"?>
<infos>
<city id="1">
<temp>20'C/30'C</temp>
<weather>多云</weather>
<wind>南风3级</wind>
<name>上海</name>
<pm>200</pm>
</city>
<city id="2">
<temp>20'C/35'C</temp>
<weather>暴雨</weather>
<wind>南风10级</wind>
<name>北京</name>
<pm>800</pm>
</city>
<city id="3">
<temp>0'C/10'C</temp>
<weather>暴雪</weather>
<wind>北风32级</wind>
<name>广州</name>
<pm>200</pm>
</city>
</infos>
2:
package com.itheima.weather;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.itheima.weather.po.WeatherInfo;
import com.itheima.weather.service.WeaterService;
public class WeatherActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv_info = (TextView) this.findViewById(R.id.vt_info);
try {
ArrayList<WeatherInfo> infos = WeaterService
.getWeatherInfo(WeatherActivity.class.getClassLoader()
.getResourceAsStream("weather.xml"));
StringBuffer sb = new StringBuffer();
for (WeatherInfo info : infos) {
sb.append(info);
sb.append("\n");
}
tv_info.setText(sb.toString());
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this,"xml解析失败",0).show();
}
}
}
3:
package com.itheima.weather.service;
import java.io.InputStream;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import com.itheima.weather.po.WeatherInfo;
public class WeaterService {
public static ArrayList<WeatherInfo> getWeatherInfo(InputStream is)
throws Exception {
ArrayList<WeatherInfo> infos = null;
WeatherInfo info = null;
// XmlPullParser parser = Xml.newPullParser();
XmlPullParserFactory fac = XmlPullParserFactory.newInstance();
XmlPullParser parser = fac.newPullParser();
parser.setInput(is,"utf-8");
int type = parser.getEventType();
while (type != XmlPullParser.END_DOCUMENT) {
switch (type) {
case XmlPullParser.START_TAG:
if ("infos".equals(parser.getName())) {
infos = new ArrayList<WeatherInfo>();
} else if ("city".equals(parser.getName())) {
info = new WeatherInfo();
info.setId(Integer.parseInt(parser.getAttributeValue(0)));
} else if ("temp".equals(parser.getName())) {
info.setTemp(parser.nextText());
} else if ("weather".equals(parser.getName())) {
info.setWeather(parser.nextText());
} else if ("wind".equals(parser.getName())) {
info.setWind(parser.nextText());
} else if ("name".equals(parser.getName())) {
info.setName(parser.nextText());
} else if ("pm".equals(parser.getName())) {
info.setPm(parser.nextText());
}
break;
case XmlPullParser.END_TAG:
if ("city".equals(parser.getName())) {
infos.add(info);
info = null;
}
break;
type = parser.next();
}
return infos;
}
}
4:
package com.itheima.weather.po;
public class WeatherInfo {
private int id;
private String temp;
private String weather;
private String wind;
private String name;
private String pm;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTemp() {
return temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
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 getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPm() {
return pm;
}
public void setPm(String pm) {
this.pm = pm;
}
@Override
public String toString() {
return "[id=" + id + ",温度=" + temp + ",天气=" + weather + ",风力="
+ wind + ",城市=" + name + ",pm2.5=" + pm + "]";
}
}