标签:
// 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();
}
标签:
原文地址:http://www.cnblogs.com/wangfeng520/p/5068818.html