首页 技术 正文
技术 2022年11月8日
0 收藏 772 点赞 1,551 浏览 3518 个字

1、要解析的xml文件

 <?xml version="1.0" encoding="utf-8"?>
<infos>
<city id="1">
<temp>20C/30C</temp>
<weather>多云转晴</weather>
<wind>7-8级</wind>
<name>广州</name>
<pm>200</pm>
</city>
<city id="2">
<temp>25C/30C</temp>
<weather>多云转晴</weather>
<wind>2-3级</wind>
<name>钦州</name>
<pm>100</pm>
</city>
<city id="3">
<temp>20C/30C</temp>
<weather>多云转晴</weather>
<wind>7-9级</wind>
<name>北海</name>
<pm>250</pm>
</city>
</infos>

2、activity代码

 package com.example.myweather; import java.util.List; import com.example.myweather.service.WeatherService;
import com.example.myweather.domain.WeatherInfo;
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(Exception e){
e.printStackTrace();
Toast.makeText(this, "解析失败!", 0).show();
} } }

3、解析xml

 package com.example.myweather.service; 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 com.example.myweather.domain.WeatherInfo; public class WeatherService { public static List<WeatherInfo> getWeatherInfos(InputStream is) throws Exception{
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;
}
default:
break;
} type = parser.next();
}
return weatherInfos;
}
}

4、weatherInfo类

 package com.example.myweather.domain; public class WeatherInfo {     private int id;
private String temp;
private String weather;
private String wind;
private String name;
private String pm; @Override
public String toString() {
return "WeatherInfo [id=" + id + ", temp=" + temp + ", weather="
+ weather + ", wind=" + wind + ", name=" + name + ", pm=" + 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;
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,112
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,585
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,922