2013-05-10 36 views
1

我正在學習製作android應用程序。我的應用程序的目標是將來自URL的XML文件中的解析數據存儲到SQLite數據庫中。我已經成功地進行了解析過程,但我希望將數據從XML存儲到SQLite,這樣我的應用程序就可以在互聯網訪問停止時打開XML數據。這是我的項目如何將XML文件從http解析到Android中的SQLite中

MainActivity.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); 
      } 
     }); 
    } 
} 
} 

我一直在尋找,發現很多教程從互聯網上我的代碼,但我不知道最好的方法,在我的應用程序中使用它。

回答

0

我建議你先'想'如何建立你的SQL表。 可能散列圖中的每個KEY都應該是列。

看到這個不錯tutorial關於sqlite。

1

首先,你必須創建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 
} 
+0

你能告訴我,爲的SQLite數據庫創建最好的方式? – endhare 2013-05-12 05:26:11

+0

使用我給你的adminSQLite類 – 2013-05-14 03:28:03