赞
踩
StringBuilder sb = new StringBuilder();
XmlResourceParser xmlParser = getResources().getXml(R.xml.books);
try {
while (xmlParser.getEventType() != XmlResourceParser.END_DOCUMENT) {
if (xmlParser.getEventType() == XmlResourceParser.START_TAG) {
String tagName = xmlParser.getName();
if (tagName.equals("book")) {
sb.append("price");
String price = xmlParser.getAttributeValue(null, "price");
sb.append(price);
sb.append("publishYear");
String pubishYear = xmlParser.getAttributeValue(1);
sb.append(pubishYear);
sb.append("bookName");
sb.append(xmlParser.nextText());
}
sb.append("\n");
}
xmlParser.next();
}
}
Book.xml
<?xml version="1.0" encoding="utf-8"?>
<books>
<book price="100" publishYear="2011">Crazy Java</book>
<book price="110" publishYear="2012">Crazy Android</book>
<book price="120" publishYear="2013">Crazy iOS</book>
</books>
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。