我想知道如何将XML数据导入sqlite? 在m应用程序中我想将我的Web服务器mysql数据插入到android mobile sqlite中...是否有另一种方法没有xml解析?
请帮助我或给我ref链接从xml(远程)将数据导入sqlite ..将非常感谢你的帮助... :)
答案 0 :(得分:4)
干草检查此代码它会帮助你
package com.pxr.tutorial.xmltest; 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(); } }