Android之之PULL、、SAX、、DOM解析解析XML
背景:解析天气预报的xml文件,在模拟器显示
解析前准备
layout目录下weather.xml
需要解析的文件:raw目录下的weather1.xml
20℃/30℃
晴天多云
上海
80
1级
26℃/32℃
晴天
北京
98
3级
15℃/24℃
多云
广州
30
5级
WeatherBean.java
package com.example.ch4;
public class WeatherBean {
private String id;
private String temp;
private String weather;
private String name;
private String pm;
private String wind;
public String getId() {
return id;
}
public void setId(String 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 getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPm() {
return pm;
}
public void setPm(String pm) {
this.pm = pm;
}
public String getWind() {
return wind;
}
public void setWind(String wind) {
this.wind = wind;
}
}
MainActivity.java将weather1.xml解析到的信息映射到布局weather.xml中
//解析天气
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tvCity;
private TextView tvWeather;
private TextView tvTemp;