·在安卓中,使用pull解析xml文件。使用方法如下:
1、准备好一个保存了天气信息的weather1.xml文件
<infos>
<city id="bj">
<temp>23</temp>
<wind>南风3级</wind>
<weather>晴</weather>
</city>
<city id="sh">
<temp>34</temp>
<wind>北风3级</wind>
<weather>阴</weather>
</city>
</infos>
2、创建一个实体类weatherInfo.java用于保存天气信息
package cn.edu.sjzc;
public class WeatherInfo {
private String city;
private String temp;
private String wind;
private String weather;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getTemp() {
return temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
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;
}
}
3、编写解析xml的工具类
package cn.edu.sjzc;
import android.util.Xml;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class WeatherService {
public static List<WeatherInfo> getWeatherInfoFromXML(InputStream is) throws Exception {
List<WeatherInfo> list = null;
WeatherInfo weatherInfo = null;
//获取pull解析器
XmlPullParser parser = Xml.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())){
//创建集合对象
list = new ArrayList<WeatherInfo>();
}else if("city".equals(parser.getName())){
//初始化天气对象
weatherInfo = new WeatherInfo();
String id = parser.getAttributeValue(0);//获得city的id属性。
weatherInfo.setCity(id);
}else if("temp".equals(parser.getName())){
String temp = parser.nextText();
weatherInfo.setTemp(temp);
}else if("wind".equals(parser.getName())){
String wind = parser.nextText();
weatherInfo.setWind(wind);
}else if("weather".equals(parser.getName())){
String weather = parser.nextText();
weatherInfo.setWeather(weather);
}
break;
case XmlPullParser.END_TAG: //结束标签
if ("city".equals(parser.getName())){
//把天气bean对象加入到集合中
list.add(weatherInfo);
}
break;
}
type = parser.next();
}
return list;
}
}
4、编写一个简单页面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/city"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView
android:id="@+id/temp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView
android:id="@+id/wind"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView
android:id="@+id/weather"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<Button
android:id="@+id/bt_bj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bj"
/>
<Button
android:id="@+id/bt_sh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sh"
/>
</LinearLayout>
5、使用在activity中使用工具类解析xml,然后给Textview的Text赋值:
package cn.edu.sjzc;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv_city;
private TextView tv_temp;
private TextView tv_wind;
private TextView tv_weather;
List<Map<String,String>> list;
private Map<String,String> map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initView();
//解析XML
InputStream inputStream = getResources().openRawResource(R.raw.weather1);//得到XMl文件
try {
List<WeatherInfo> weatherInfos = WeatherService.getWeatherInfoFromXML(inputStream);
list = new ArrayList<>();
for (WeatherInfo weatherInfo:weatherInfos) {
map = new HashMap<>();
map.put("city",weatherInfo.getCity());
map.put("temp",weatherInfo.getTemp());
map.put("wind",weatherInfo.getWind());
map.put("weather",weatherInfo.getWeather());
list.add(map);
}
} catch (Exception e) {
e.printStackTrace();
}
//自定义方法,讲天气信息传到Textview中
getMap(0);
}
private void getMap(int number) {
Map<String,String> oneMap = list.get(number);//取出一个mapper
String city = oneMap.get("city");
String temp = oneMap.get("temp");
String wind = oneMap.get("wind");
String weather = oneMap.get("weather");
tv_city.setText(city);
tv_temp.setText(temp);
tv_wind.setText(wind);
tv_weather.setText(weather);
}
private void initView() {
tv_city= findViewById(R.id.city);
tv_temp = findViewById(R.id.temp);
tv_wind = findViewById(R.id.wind);
tv_weather = findViewById(R.id.weather);
findViewById(R.id.bt_bj).setOnClickListener(this);
findViewById(R.id.bt_sh).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_bj://说明点击的是北京
getMap(0);
break;
case R.id.bt_sh:
getMap(1);
break;
}
}
}
6、运行查看效果: