xml的pull解析:
//类加载器加载xml文件
InputStream is = MainActivity.class.getClassLoader().getResourceAsStream("weather.xml");
//生成xml的pull解析器
XmlPullParser pull = Xml.newPullParser();
try {
//设置输入流
pull.setInput(is, "utf-8");
//解析器当前处于的状态
int type = pull.getEventType();
Weather weather = null ;
while (type != XmlPullParser.END_DOCUMENT) {
switch (type) {
//当前是开始标签的节点
case XmlPullParser.START_TAG:
//拿到节点标签的名字
String name = pull.getName();
//判断节点标签的名字 (下面同这儿),然后放到weather对象中
if("channel".equals(name)){
weather = new Weather();
weather.setId(pull.getAttributeValue(0));
}
if("city".equals(name)){
weather.setName(pull.nextText());
}
if("temp".equals(name)){
weather.setTemp(pull.nextText());
}
if("wind".equals(name)){
weather.setWind(pull.nextText());
}
break;
//如果是结束标签的节点,此处将weather对象放入list集合保存
case XmlPullParser.END_TAG:
String tagname = pull.getName();
if("channel".equals(tagname)){
list.add(weather);
}
default:
break;
}
//此处必须写,否则不会向下执行
type = pull.next();
}
} catch (Exception e) {
e.printStackTrace();
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。