weather.xml
<?xml version="1.0" encoding="GBK"?>
<infos>
<city id="1">
<temp>20℃/30℃</temp>
<weather>5月20日 多云转阴</weather>
<wind>南风3-4级</wind>
<name>上海</name>
<pm>200</pm>
</city>
<city id="2">
<temp>20℃/30℃</temp>
<weather>5月20日 多云转晴</weather>
<wind>南风7-8级</wind>
<name>合肥</name>
<pm>800</pm>
</city>
<city id="3">
<temp>6℃/10℃</temp>
<weather>5月20日 多云转雨夹雪</weather>
<wind>南风2-6级</wind>
<name>哈尔滨</name>
<pm>100</pm>
</city>
</infos>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
WeatherInfo.java
package com.demo.weather;
public class WeatherInfo {
private int id;
private String name;
private String wind;
private String weather;
private String temp;
private String pm;
@Override
public String toString() {
return "天气信息 [城市编号=" + id + ", 城市名称=" + name + ", 城市风度=" + wind
+ ", 城市天气=" + weather + ", 神马=" + temp + ", 舒适度吧=" + pm
+ "]";
}
public WeatherInfo() {
super();
}
public WeatherInfo(int id, String name, String wind, String weather,
String temp, String pm) {
super();
this.id = id;
this.name = name;
this.wind = wind;
this.weather = weather;
this.temp = temp;
this.pm = pm;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWind() {
return wind;
}
public void setWind(String wind) {
this.wind = wind;
}
public String getWeather() {
return weather;
}
public void setWeather(String weather) {
this.weather = weather;
}
public String getTemp() {
return temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
public String getPm() {
return pm;
}
public void setPm(String pm) {
this.pm = pm;
}
}
WeatherService.java
package com.demo.weather;
/**
* DOM 加载内存,生成一个树状结构,消耗内存比较大
* SAX 基于时间的方式 ,速度快,效率高,不能倒退
* Pull解析
*/
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Xml;
import android.widget.ArrayAdapter;
public class WeatherService {
public static List<WeatherInfo> getWeatherInfos(InputStream is) throws XmlPullParserException, IOException{
XmlPullParser parser = Xml.newPullParser();
//初始化解析器
parser.setInput(is,"utf-8");
List<WeatherInfo> weatherInfos = null;
WeatherInfo weatherInfo = null;
int type = parser.getEventType();
while(type != XmlPullParser.END_DOCUMENT){
switch (type) {
case XmlPullParser.START_TAG:
if("infos".equals(parser.getName())){
//解析到全局开始的标签
weatherInfos = new ArrayList<WeatherInfo>();
}else if("city".equals(parser.getName())){
weatherInfo = new WeatherInfo();
String idStr = parser.getAttributeValue(0);
weatherInfo.setId(Integer.parseInt(idStr));
}else if("temp".equals(parser.getName())){
String temp = parser.nextText();
weatherInfo.setTemp(temp);
}else if("weather".equals(parser.getName())){
String weather = parser.nextText();
weatherInfo.setWeather(weather);
}else if("wind".equals(parser.getName())){
String wind = parser.nextText();
weatherInfo.setWind(wind);
}else if("name".equals(parser.getName())){
String name = parser.nextText();
weatherInfo.setName(name);
}else if("pm".equals(parser.getName())){
String pm = parser.nextText();
weatherInfo.setPm(pm);
}
break;
case XmlPullParser.END_TAG:
if("city".equals(parser.getName())){
//一个城市的信息已经处理完毕了
weatherInfos.add(weatherInfo);
weatherInfo = null;
}
break;
}
type = parser.next();
}
return weatherInfos;
}
}
MainActivity.java
package com.demo.weather;
import java.io.IOException;
import java.util.List;
import org.xmlpull.v1.XmlPullParserException;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.tv);
try {
List<WeatherInfo> infos = WeatherService.getWeatherInfos(MainActivity.class.getClassLoader().getResourceAsStream("weather.xml"));
StringBuffer sb = new StringBuffer();
for(WeatherInfo info:infos){
String str = info.toString();
sb.append(str);
sb.append("\n");
}
tv.setText(sb.toString());
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, "解析失败", 0).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, "解析失败", 0).show();
}
}
}