首页 > 其他 > 详细

XML解析

时间:2015-12-23 09:17:21      阅读:153      评论:0      收藏:0      [点我收藏+]

// xml,资源解析器,解析本地文件
XmlResourceParser parser = getResources().getXml(R.xml.customer);
try {
int type = parser.getEventType();// 解析到的类型
while (type != XmlResourceParser.END_DOCUMENT) {// 没有解析到结尾,有些地方没有内容为空
if (type == XmlResourceParser.START_TAG) {// 标签开始的地方<
String tagname = parser.getName();// 标签名
if (tagname.equals("student")) {
student = new Student();
student.setId(parser.getAttributeValue(0));
student.setName(parser.getAttributeValue(1));
student.setAge(Integer.parseInt(parser
.getAttributeValue(2)));
students.add(student);
}else if (tagname.equals("teacher")) {
teacher = new Teacher();
teacher.setId(parser.getAttributeValue(0));
teacher.setName(parser.getAttributeValue(1));
teacher.setAge(Integer.parseInt(parser
.getAttributeValue(2)));
teachers.add(teacher);
}else if (tagname.equals("customer")) {
customer = new Customer();
customer.setId(parser.getAttributeValue(0));
customer.setName(parser.getAttributeValue(1));
customer.setAge(Integer.parseInt(parser
.getAttributeValue(2)));
customers.add(customer);
}
}
type = parser.next();// 继续解析
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

XML解析

原文:http://www.cnblogs.com/wangfeng520/p/5068818.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!