提前创建一个xml文件
命名为weather.xml 用来代替服务器 传入的内容
内容
<?xml version="1.0" encoding="utf-8"?>
<weather>
<channel id="1">
<city>北京</city>
<temp>25°</temp>
<wind>3</wind>
<pm250>300</pm250>
</channel>
<channel id="2">
<city>汕头</city>
<temp>20°</temp>
<wind>4</wind>
<pm250>50</pm250>
</channel>
<channel id="3">
<city>广州</city>
<temp>30°</temp>
<wind>4</wind>
<pm250>100</pm250>
</channel>
<channel id="4">
<city>深圳</city>
<temp>25°</temp>
<wind>1</wind>
<pm250>50</pm250>
</channel>
</weather>
android studio 创建一个项目是不会创建 assets 资产管理文件的
所以要我们自己创建 (eclipse是会自动创建的)在app文件夹右键
以上创建过程
创建完在src/main/下就会出现assets文件夹
然后添加开头的weather.xml文件
进入activity_main.xml
默认都会有这一句
然后添加
转到MainActivity.java文件
路径src\main\java\下
添加这一句findViewById(R.id.tv_weather); 找到我们之前修改的文本
实质是一个TextView 通过强制类型转换变成一下
这一步是为了显示信息
然后我们做一个解析的工具类 单独抽出来
命名为WeatherParser 返回值为List 看到这个就知道 我们要单独创建一个对象Channel
对象内容对应着我们创建的weather.xml
public class WeatherParser {
public static List<Channel> parserXml(InputStream in) throws Exception{
List<Channel> weatherLists = null;
Channel channel = null;
// 1获取Xmlpullparser解析的实例
XmlPullParser parser = Xml.newPullParser();
// 2设置Xmlpullparser 的参数
parser.setInput(in,"utf-8");
//获取事件类型
int type = parser.getEventType();
while(type!=XmlPullParser.END_DOCUMENT){
switch (type){
case XmlPullParser.START_DOCUMENT:
//具体判断一下解析
weatherLists = new ArrayList<Channel>();
break;
case XmlPullParser.START_TAG://解析开始标签
if("channel".equals(parser.getName())){
channel = new Channel();
String id = parser.getAttributeName(0);
channel.setId(id);
}else if("city".equals(parser.getName()) ){
//获取标签的下一个 不是getText()他直接就取了city作为值
String city = parser.nextText();
channel.setCity(city);
}else if("temp".equals(parser.getName()) ){
String temp = parser.nextText();
channel.setTemp(temp);
}else if("wind".equals(parser.getName()) ){
String wind = parser.nextText();
channel.setWind(wind);
}else if("pm250".equals(parser.getName()) ){
String pm250 = parser.nextText();
channel.setPm250(pm250);
}
break;
case XmlPullParser.END_TAG://解析结束标签
if("channel".equals(parser.getName())){
weatherLists.add(channel);
}
break;
}
type = parser.next();
}
return weatherLists;
}
}
这里是分割线--------------------------------------------------------------------------
/**
* Created by HAHA on 2018/9/25.
*/
public class Channel {
//这对应着我们创建的weather.xml 里面的内容
private String id;
private String city;
private String temp;
private String wind;
private String pm250;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 getPm250() {
return pm250;
}
public void setPm250(String pm250) {
this.pm250 = pm250;
}
@Override
public String toString() {
return "Channel{" +
"id='" + id + '\'' +
", city='" + city + '\'' +
", temp='" + temp + '\'' +
", wind='" + wind + '\'' +
", pm250='" + pm250 + '\'' +
'}';
}
}
如果对WeatherParser有不懂的可以对应文档中的
https://developer.android.google.cn/reference/org/xmlpull/v1/XmlPullParser
这里面有官方代码 可以参考
要特别注意这两个地方 第一个是要单独抽出来
第二个看注释
到这里解析工具就完成
开始将数据展示到TextView上 回到MainActivity里
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// 1.显示信息
TextView tv_weather = (TextView) findViewById(R.id.tv_weather);
//1.1获取资产管理者 通过上下文
InputStream inputStream = getAssets().open("weather.xml");
List<Channel> weatherlists = WeatherParser.parserXml(inputStream);
StringBuffer sb = new StringBuffer();
for (Channel channel:weatherlists){
sb.append(channel.toString());
}
tv_weather.setText(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
然后点击运行
显示成功