从 xml(remote) Android 将数据导入到 sqlite

发布于 2024-11-02 04:30:20 字数 172 浏览 1 评论 0原文

我想知道如何将XML数据导入到sqlite? 在 m 应用程序中,我想将我的 Web 服务器 mysql 数据插入到 android mobile sqlite...是否有另一种无需 xml 解析的方法?

请帮助我或给我从 xml(remote) 将数据导入到 sqlite 的参考链接..将感谢您的帮助...:)

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 ?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

能否归途做我良人 2024-11-09 04:30:20

干草检查这个代码它会对你有帮助

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




}

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




}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文