XML from WWW to SQLite

N

NewAndroider

Neues Mitglied
0
Hallo Androidaner!
Dank an jeden, der seine Zeit für meine Frage "opfert"!
Meine Aufgabe ist, eine XML von Internetadresse holen und mit SQLite DB abgleichen. Inhalt SQLite in ListActivity zur Auswahl anbieten und nach Userauswahl in extra Activity den Content aufbereitet anzeigen.
SQLite DB Tabelle erstellen, Import, löschen hab ich schon geübt, abgleich fehlt mir. Aber wie importiere ich die XML in den raw Ordner und wie gleiche ich die die XML mit der SQLite ab? Für jeden "Schupps" dankbar! Sepp:smile:


'###########Hab ein Beispiel gefunden............. gibt's besseres?

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.ListActivity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends ListActivity {
//private static final String TAG = "com.pxr.tutorial.xmltest.Main";
private SQLiteDatabase database;
//static final String ID="id";
static final String NAME="name";
static final String SCORE="score";
TextView error;

// NEVER MORE DO THE SAME!!!
// public void onCreate(SQLiteDatabase db) {
// db.execSQL("CREATE TABLE constants (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, score INTEGER);");
//
// ContentValues cv=new ContentValues();
//
// //Test data
// cv.put(NAME, "Ajay");
// cv.put(SCORE, "100");
// db.insert("constants", null, cv);
//
// }

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);

//IT'S OPENNING DATABASE IF DATABASE DOESYNT EXIST IT'S CREATING IT
database = (new DatabaseHelper(this).getWritableDatabase());

//BETTER CREATE YOU OWN CLASS WITH FIELDS AND ACCESSORS
ArrayList> mylist = new ArrayList>();


String xml = XMLfunctions.getXML();
Document doc = XMLfunctions.XMLfromString(xml);

int numResults = XMLfunctions.numResults(doc);

if((numResults map = new HashMap();

Element e = (Element)nodes.item(i);
map.put("id", XMLfunctions.getValue(e, "id"));
map.put("name", XMLfunctions.getValue(e, "name"));
map.put("Score", XMLfunctions.getValue(e, "score"));

mylist.add(map);
}

//READ SOMETING ABOUT ITERATORS AND COLLECTIONS IN JAVA
for (int j = 0; j parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap o = (HashMap) lv.getItemAtPosition(position);
Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_LONG).show();

}
});*/
}

private void processAdd(String string, String string2, String string3) {

// TODO Auto-generated method stub
ContentValues values = new ContentValues(2);
// values.put("id", string);
values.put(DatabaseHelper.NAME, string2);
values.put(DatabaseHelper.SCORE, string3);
if(database!=null){
//INSERT IF DATABASE IS OPEN
database.insert(DatabaseHelper.TABLE_NAME, null, values);
// Log.i(TAG, "SAVED IN DATABASE");
}else{
//IF DATABASE IS CLOSED OPEN FIRST THAN INSERT
database = (new DatabaseHelper(this).getWritableDatabase());
//Log.e(TAG, "DATABASE CLOSED, OPENNING...");
database.insert(DatabaseHelper.TABLE_NAME, null, values);
//Log.i(TAG, "INSERTED INTO DATABASE");

}


}

@Override
protected void onDestroy() {
//ALWAYS CLOSE DATABASE IF YOU ARE FINISHING APPLICATION
if(database!=null){
database.close();
}
super.onDestroy();
}
}
 
Zuletzt bearbeitet:

Ähnliche Themen

R
Antworten
6
Aufrufe
1.025
swa00
swa00
M
Antworten
5
Aufrufe
1.088
markusk73
M
Zurück
Oben Unten