***************************************************************XML解析之DOM解析_四层结合数据库*********************************************************************
*******************************文件放在assets中*************************
**************************************************数据库的Bean**************************************************************
public class Book { private String _id; private String name; private String author; private String price; public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public Book(String _id,String name,String author,String price) { super(); this._id = _id; this.name = name; this.author = author; this.price = price; } public Book() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return name + "\t\t"+ "\t\t"+ author+ "\t\t" + price + "\n"; } public Book(String name,String price) { super(); this.name = name; this.author = author; this.price = price; } }
import java.util.ArrayList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import android.os.Bundle; import android.app.Activity; import android.content.ContentValues; import android.database.sqlite.sqliteDatabase; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { private View listbook; private ListView listView; private ArrayList<Book> list; private ArrayAdapter<Book> adapter; private MysqLite MysqLite; private sqliteDatabase db; private ContentValues values; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listbook = findViewById(R.id.listbook); listView = (ListView) findViewById(R.id.listView); list = new ArrayList<Book>(); MysqLite = new MysqLite(this); db = MysqLite.getWritableDatabase(); values = new ContentValues(); adapter = new ArrayAdapter<Book>(MainActivity.this,android.R.layout.simple_list_item_1,android.R.id.text1,list); listView.setAdapter(adapter); listbook.setOnClickListener(new OnClickListener() { private String price; private String name; private String author; @Override public void onClick(View v) { list.clear();//每次都清空数据 try { //获取一个DocumentBuilder工厂 DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); //通过DocumentBuilder工厂得到一个DocumentBuilder对象 DocumentBuilder builder = factory.newDocumentBuilder(); //getAssets方法获取资源文件,使用DocumentBuilder对象将资源文件流转换成Document对象 Document document = builder.parse(getAssets().open( "books2.xml")); //使用document对象的getdocumentElement方法获取document第一层Element对象 Element element = document.getDocumentElement(); //通过element对象的getelementsByTagName获取该element子节点的nodeList NodeList china = element.getElementsByTagName("china"); for (int i = 0; i < china.getLength(); i++) { Element china1 = (Element) china.item(i); NodeList list2 = element.getElementsByTagName("book"); //遍历nodeList for (int j = 0; j < list2.getLength(); j++) { Element element2 = (Element) list2.item(j); // String id = element2.getAttribute("id"); price = element2.getElementsByTagName("price") .item(0).getTextContent(); name = element2.getElementsByTagName("name") .item(0).getFirstChild().getNodeValue(); author = element2.getElementsByTagName("author") .item(0).getTextContent(); //bookHe(); } } NodeList foreiner = element.getElementsByTagName("foreiner"); for (int i = 0; i < foreiner.getLength(); i++) { Element foreiner1 = (Element) foreiner.item(i); NodeList list2 = element.getElementsByTagName("book"); //遍历nodeList for (int j = 0; j < list2.getLength(); j++) { Element element2 = (Element) list2.item(j); // String id = element2.getAttribute("id"); price = element2.getElementsByTagName("price") .item(0).getTextContent(); name = element2.getElementsByTagName("name") .item(0).getFirstChild().getNodeValue(); author = element2.getElementsByTagName("author") .item(0).getTextContent(); bookHe(); values.put("name",name);//添加到数据库 values.put("author",author); values.put("price",price); db.insert("book",null,values); Toast.makeText(MainActivity.this,"BOOK",0).show(); } } Toast.makeText(MainActivity.this,"KING",0).show(); adapter.notifyDataSetChanged(); } catch (Exception e) { // TODO: handle exception } } private void bookHe() { Book book = new Book(name,author,price); list.add(book); } }); } }
**************************************************MysqLite.class****************************************************************
import android.content.Context; import android.database.sqlite.sqliteDatabase; import android.database.sqlite.sqliteDatabase.CursorFactory; import android.database.sqlite.sqliteOpenHelper; public class MysqLite extends sqliteOpenHelper { public MysqLite(Context context) { super(context,"k.db",1); // TODO Auto-generated constructor stub } @Override public void onCreate(sqliteDatabase db) { // TODO Auto-generated method stub db.execsql("create table book(_id integer primary key autoincrement,name varchar(0),author varchar(0),price varchar(0))"); } @Override public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) { // TODO Auto-generated method stub } }原文链接:https://www.f2er.com/xml/295054.html