Android:读取assets目录下文件(二)
在上篇博客里,介绍了如何读取assets目录下的txt文件。在这里,介绍如何读取xml文件。
xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="001">
<title>Harry Potter</title>
<price>¥35.00</price>
</book>
<book id="002">
<title>Learning XML</title>
<price>¥66.00</price>
</book>
<book id="003">
<title>Thinking in Java</title>
<price>¥1.00</price>
</book>
<book id="004">
<title>Useful Android</title>
<price>¥1.50</price>
</book>
</books>
根据xml文件,接下来定义一个Book类,有id, title, price三个属性。
然后使用一个子线程,通过AssetManager读取到数据流,再使用XmlPullParser解析xml文件。
完整示例代码如下:
package com.li.readassetfile;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.content.res.AssetManager;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.os.Handler;
import android.util.Xml;
import android.widget.TextView;
public class ReadXmlActivity extends Activity {
private TextView tvContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_xml);
tvContent = (TextView) findViewById(R.id.tvContent);
new Handler().post(new Runnable() {
@Override
public void run() {
AssetManager manager = getResources().getAssets();
List<Book> books = null;
Book book = null;
try {
InputStream inputStream = manager.open("book.xml");
XmlPullParser parser = Xml.newPullParser();
parser.setInput(inputStream, "UTF-8");
StringBuilder sb = new StringBuilder();
try {
while(parser.getEventType() != XmlPullParser.END_DOCUMENT){
if(parser.getEventType() == XmlPullParser.START_DOCUMENT){
books = new ArrayList<ReadXmlActivity.Book>();
}else if(parser.getEventType() == XmlPullParser.START_TAG){
if("book".equals(parser.getName())){
book = new Book();
book.setId(parser.getAttributeValue(0));
}
if(book!=null){
if("title".equals(parser.getName())){//判断开始标签元素是否是name
book.setTitle(parser.nextText());
}else if("price".equals(parser.getName())){//判断开始标签元素是否是price
book.setPrice(parser.nextText());
}
}
}else if(parser.getEventType() == XmlPullParser.END_TAG){
if("book".equals(parser.getName())){//判断结束标签元素是否是book
books.add(book);//将book添加到books集合
book = null;
}
}
parser.next();
}
if(books != null && books.size() > 0){
for(int i = 0; i < books.size(); i ++){
sb.append(books.get(i).getId() + "\b\b");
sb.append(books.get(i).getTitle() + "\b\b");
sb.append(books.get(i).getPrice() + "\b\b");
sb.append("\n");
}
}
inputStream.close();
tvContent.setText(sb.toString());
} catch (XmlPullParserException e) {
e.printStackTrace();
}
} catch (IOException | XmlPullParserException e) {
e.printStackTrace();
}
}
});
}
private class Book{
private String id;
private String title;
private String price;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
}
使用完,要注意关闭数据流。为了防止ANR,所以这里使用子线程来读取xml文件。