如何解析assets目录下的xml文件,,并展示

//主界面

package com.example.xmlpull14_2;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import org.xml.sax.Parser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;


import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Xml;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;




public class MainActivity extends Activity {
//定义集合,用来接收解析的对象
private List<Book> list_book;
// private TextView tv;
    private ListView mListView;
    
    private SQLiteDatabase sd;
    private BookOpenHelper mBookOpenHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBookOpenHelper=new BookOpenHelper(getApplicationContext(), "pullxml.db", null, 1);
        sd=mBookOpenHelper.getReadableDatabase();
        
        
        mListView=(ListView) findViewById(R.id.mylistiew);
//        tv=(TextView) findViewById(R.id.tv);
        try {
        intodata();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//        tv.setText(list_book.toString());
        mListView.setAdapter(adapter);
    }


    
    
private void intodata() throws XmlPullParserException, IOException {
Book book=null;
String tagName="";

XmlPullParser  parser=Xml.newPullParser();
//
parser.setInput(getAssets().open("books.xml"), "utf-8");
//定义一个事件类型
int eventtype=parser.getEventType();

while (eventtype!=parser.END_DOCUMENT){
switch (eventtype) {
case XmlPullParser.START_DOCUMENT:

break;
case XmlPullParser.START_TAG:
tagName=parser.getName();
if (tagName.equals("books")) {
list_book=new ArrayList<Book>();
}else if(tagName.equals("book")){
book=new Book();
String id=parser.getAttributeValue(0);
book.setId(Integer.parseInt(id));
}  
break;
case XmlPullParser.TEXT:
String content=parser.getText();
if (tagName.equals("name")) {
book.setName(content);
}else if(tagName.equals("price")){
book.setPrice(content);
}else if(tagName.equals("type")){
book.setType(content);
}
break;
case XmlPullParser.END_TAG:
tagName=parser.getName();
if (tagName.equals("book")) {
list_book.add(book);
sd.execSQL("insert into book(id,name,price,type)values(null,?,?,?)", new Object[]{book.getName(),book.getPrice(),book.getType()});
book=null;
}
tagName="";
break;
case XmlPullParser.END_DOCUMENT:

break;
default:
break;
}

eventtype=parser.next();
}
}
/**
 * 定义适配器
 */
  BaseAdapter adapter=new BaseAdapter() {

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder v;
if (convertView==null) {
v=new ViewHolder();
convertView=LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_item, null);
v.name=(TextView) convertView.findViewById(R.id.name);
v.price=(TextView) convertView.findViewById(R.id.price);
v.type=(TextView) convertView.findViewById(R.id.type);
convertView.setTag(v);
}else{
v=(ViewHolder) convertView.getTag();
}
v.name.setText(list_book.get(position).getName());
v.price.setText(list_book.get(position).getPrice());
v.type.setText(list_book.get(position).getType());
return convertView;
}
 
class ViewHolder{
TextView name,price,type;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list_book.get(position);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return list_book.size();
}
};
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值