开发者

Importing data to sqlite from xml(remote) Android

开发者 https://www.devze.com 2023-02-25 08:43 出处:网络
i want to know how to import XML data to sqlite? In m application i want to insert my web server mysql data to android mobile sqlite... is there another way without xml parsing ?开发者_Go百科

i want to know how to import XML data to sqlite? In m application i want to insert my web server mysql data to android mobile sqlite... is there another way without xml parsing ?开发者_Go百科

Please help me or give me ref link to Importing data to sqlite from xml(remote).. will appreciate your help ...:)


hay check this code it will help you

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();
    }




}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号