gpt4 book ai didi

android - 如何在 Android 中将 XML 文件从 http 解析为 SQLite

转载 作者:行者123 更新时间:2023-11-30 03:37:12 25 4
gpt4 key购买 nike

我正在学习制作 Android 应用程序。我的应用程序的目标是将来自 URL 的 XML 文件的解析数据存储到 SQLite 数据库中。我已经成功完成解析过程,但我想将数据从 XML 存储到 SQLite,以便我的应用程序可以在 Internet 访问中断时打开 XML 数据。这是我项目中的代码

主 Activity .java

public class MainActivity extends ListActivity {
static final String URL = "http://data.bmkg.go.id/propinsi_15_2.xml";
static final String KEY_ITEM = "Cuaca";
static final String KEY_ID = "Isi";
static final String KEY_ROW = "Row";
static final String KEY_KOTA = "Kota";
static final String KEY_LINTANG = "Lintang";
static final String KEY_BUJUR = "Bujur";
static final String KEY_CUACA = "Cuaca";
static final String KEY_SUHUMIN = "SuhuMin";
static final String KEY_SUHUMAX = "SuhuMax";
static final String KEY_KELEMBAPANMIN = "KelembapanMin";
static final String KEY_KELEMBAPANMAX = "KelembapanMax";
static final String KEY_KECEPATANANGIN = "KecepatanAngin";
static final String KEY_ARAHANGIN = "ArahAngin";
private ProgressDialog pDialog;

ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String,String>>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new AmbilData().execute();
}

class AmbilData extends AsyncTask<String, String, String>{
protected void onPreExecute(){
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading, Mohon Menunggu Beberapa saat...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}

protected String doInBackground (String... args){
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL);
Document doc = parser.getDomElement(xml);

NodeList nl = doc.getElementsByTagName(KEY_ROW);
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();

Element e = (Element) nl.item(i);

map.put(KEY_KOTA, parser.getValue(e, KEY_KOTA));
map.put(KEY_LINTANG, parser.getValue(e, KEY_LINTANG));
map.put(KEY_BUJUR, parser.getValue(e, KEY_BUJUR));
map.put(KEY_CUACA, parser.getValue(e, KEY_CUACA));
map.put(KEY_SUHUMIN, parser.getValue(e, KEY_SUHUMIN));
map.put(KEY_SUHUMAX, parser.getValue(e, KEY_SUHUMAX));
map.put(KEY_KELEMBAPANMIN, parser.getValue(e, KEY_KELEMBAPANMIN));
map.put(KEY_KELEMBAPANMAX, parser.getValue(e, KEY_KELEMBAPANMAX));
map.put(KEY_KECEPATANANGIN, parser.getValue(e, KEY_KECEPATANANGIN));
map.put(KEY_ARAHANGIN, parser.getValue(e, KEY_ARAHANGIN));

menuItems.add(map);
}
return null;
}
protected void onPostExecute(String file_url){
pDialog.dismiss();
runOnUiThread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
ListAdapter adapter = new SimpleAdapter(MainActivity.this, menuItems, R.layout.list_kota, new String[] {KEY_KOTA, KEY_CUACA, KEY_LINTANG, KEY_BUJUR, KEY_SUHUMIN, KEY_SUHUMAX, KEY_KELEMBAPANMIN, KEY_KELEMBAPANMAX, KEY_KECEPATANANGIN, KEY_ARAHANGIN}, new int[] {R.id.kota, R.id.cuaca, R.id.lintang, R.id.bujur, R.id.suhumin, R.id.suhumax, R.id.kelembapanmin, R.id.kelembapanmax, R.id.kecepatanangin, R.id.arahangin});
setListAdapter(adapter);
}
});
}
}
}

我一直在搜索并从互联网上找到了很多教程,但我不知道在我的应用程序中使用哪种方法最好。

最佳答案

首先你必须创建 SQLite 管理员,adminSQLite.java:

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class adminSQLite extends SQLiteOpenHelper {

public adminSQLite(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {//if DB doesn't exists create it

db.execSQL("create table your_table(column1 text,column2 text, column3 text,column4 text)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {//if version is newer, upgrade the DB
db.execSQL("drop table if exists your_table");//delete the old table
db.execSQL("create table your_table(column1 text,column2 text, column3 text,column4 text, column5 text)");//create the new table

}

}

之后,你应该在你的代码中调用这个类

int versionDB=1;
adminSQLite admin = new adminSQLite(this, "DBname", null,
versionDB);
SQLiteDatabase bd = admin.getWritableDatabase();//Open DB in write mode

并插入数据:

bd.execSQL("insert into your_table values('"+XMLValue1+"','"+XMLValue2+"','"+XMLValue3+"','"+XMLValue4+"')");

您也可以使用 ContentValues 插入:

 ContentValues registers = new ContentValues();//be sure that you're getting your XML data in Strings var
registers.put("column1", XMLValue1);
registers.put("column2", XMLValue2);
registers.put("column3", XMLValue3);
registers.put("column4", XMLValue4);
bd.insert("your_table", null, registers);
bd.close();

如果想在插入后获取数据:

Cursor data= bd.rawQuery("Select * from your_table",null);
if (data.moveToFirst()){
do{
String data1=data.getString(0);//column index
String data2=data.getString(1);
String data3=data.getString(2);
String data4=data.getString(3);
}while (data.moveToNext());
}
else{
//no data
}

关于android - 如何在 Android 中将 XML 文件从 http 解析为 SQLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16486452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com