从我找到的所有教程(包括 Stackoverflow 中的答案)中,我找不到完整的教程来展示如何实现 XML 到 SQLite 的填充。我创建了一个数据库助手,但我不知道如何将它集成到我的活动中。我想做的是;一旦应用程序启动,它应该使用 XML 填充数据库。
XML
我在“res\xml”文件夹中有一个 XML 文件“amawal_posts.xml”,其中包含一些我想在数据库中填充的条目。
<?xml version="1.0" encoding="utf-8"?>
<database name="npma_amawal" >
<!-- Table wp_posts -->
<table name="wp_posts" >
<column name="ID" >948</column>
<column name="post_content" >اورغ</column>
<column name="post_title" >ure</column>
</table>
<table name="wp_posts" >
<column name="ID" >46</column>
<column name="post_content" >adlis g llan iwaliwn FR: dictionnaire.</column>
<column name="post_title" >amawal</column>
</table>
</database>
此 XML 包含 4000 多条记录。
SQLiteOpenHelper
这是“XMLtoSQLite.java”的内容
package com.np.amawalandroiddb;
import java.io.IOException;
import org.xmlpull.v1.XmlPullParserException;
import com.np.amawalandroiddb.R;
import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class XMLtoSQLite extends SQLiteOpenHelper {
private final Context fContext;
// Set TAG for error catching
public static String TAG = "XMLtoSQLite";
// Set database columns
public static String column_ID = null;
public static String column_post_content = null;
public static String column_post_title = null;
public XMLtoSQLite(Context context) {
super(context, "amawal", null, 1);
fContext = context;
}
public void createDataBase (SQLiteDatabase db) throws IOException {
db.execSQL("CREATE TABLE amawal_posts (" + "ID INTEGER PRIMARY KEY,"
+ "post_content TEXT," + "post_content TEXT" + ");");
// Add default records amawal_posts
ContentValues Columns = new ContentValues();
// Get XML resource file
Resources res = fContext.getResources();
// Open XML file
int eventType = -1;
while (eventType != XmlResourceParser.END_DOCUMENT) {
XmlResourceParser database = res.getXml(R.xml.amawal_posts);
String name = database.getText();
Log.d(TAG, name);
try {
if (database.getEventType() == XmlResourceParser.START_TAG)
{
String s = database.getName();
if (s.equals("table"))
{
database.next(); // moving to the next node
if (database.getName() != null && database.getName().equalsIgnoreCase ( "column"))
{
column_ID = database.getText(); // to get value getText() method should be used
database.next();
column_post_content = database.getText();
database.next();
column_post_title = database.getText();
// Insert the values inside the DB
Columns.put("ID", column_ID);
Columns.put("post_content", column_post_content);
Columns.put("post_title", column_post_title);
db.insert("amawal", null, Columns);
}
Log.d(TAG, column_ID);
Log.d(TAG, column_post_content);
Log.d(TAG, column_post_title);
}
}
}
//Catch errors
catch (XmlPullParserException e)
{
Log.e(TAG, e.getMessage(), e);
}
catch (IOException e)
{
Log.e(TAG, e.getMessage(), e);
}
finally
{
//Close the XML file
database.close();
}
}
}
/* Update database to latest version */
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Crude update, make sure to implement a correct one when needed.
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS animals");
onCreate(db);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
}
我的主要活动
“MainActivity.java”的内容
package com.np.amawalandroiddb;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Begin DB work
XMLtoSQLite db = new XMLtoSQLite(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
作为 Java 编程的初学者,活动/布局/服务之间的关系......有点难以理解:(