加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xmlpull读取xml文件

发布时间:2020-12-16 09:31:47 所属栏目:百科 来源:网络整理
导读:1:weather.xml ?xml version="1.0" encoding="utf-8"? infos city id="1" temp20'C/30'C/temp weather多云/weather wind南风3级/wind name上海/name pm200/pm /city city id="2" temp20'C/35'C/temp weather暴雨/weather wind南风10级/wind name北京/name p

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 + "]";
}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读