Android sqlite 和 SaxParser

发布于 2024-10-21 15:18:14 字数 2048 浏览 1 评论 0原文

此刻我有点困惑。我收到一个 XML 文件并希望将该内容存储在 sqlite 数据库中。我想听起来很常见。我有一个 DBAdapter ,它基本上执行以下操作

private  static class DatabaseHelper extends SQLiteOpenHelper 
{
    private final Context ctx;

    DatabaseHelper(Context context) 
    {
        super(context, Constants.DB_NAME, null, Constants.DB_VERSION);
        this.ctx = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        db.execSQL("create table mytable (_id integer primary key autoincrement, field text not null, field2 text not null );");
        fillDb(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
    //not relevant here so skip it
    }

    private void fillDb(SQLiteDatabase db)
    {
        InputStream rawInput = this.ctx.getResources().openRawResource(R.raw.data);

        SAXParserFactory factory = SAXParserFactory.newInstance();
        try
        {
            SAXParser saxParser = factory.newSAXParser();
            saxParser.parse(rawInput, new HandlerXmlData());
        }
        catch(Exception e)
        {
        //not relevant here so skip it              
        }


     //this manual entries would work here
        //ContentValues values = new ContentValues();
        //values.put("field", "aaa");
        //values.put("field2", "bbb");
        //db.insert("mytable", null, values);
                }

现在的问题是在我的 SAXParser

公共类 HandlerXmlData 中扩展了 DefaultHandler {

private String currentValue = "";

public void startDocument()
{

}

public void startElement(String namespaceURI, String localName, String qName, Attributes atts) 
{
}

public void characters(char[] ch, int start, int length)
{
    currentValue = new String(ch, start, length);
    //here I get the value from my XML
              //so I need something like db.insert(...)
}

public void endElement(String uri, String localName, String qName)
{

}

因此,在我的 SAXParser 中,我无法访问 DatabaseHelper 类中的 db 对象。我如何将这些结合在一起?

谢谢,A.

at the moment I am a bit confused. I receive an XML file and want to store that content in a sqlite db. Sounds very common I guess. I have a DBAdapter which basically does the following

private  static class DatabaseHelper extends SQLiteOpenHelper 
{
    private final Context ctx;

    DatabaseHelper(Context context) 
    {
        super(context, Constants.DB_NAME, null, Constants.DB_VERSION);
        this.ctx = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        db.execSQL("create table mytable (_id integer primary key autoincrement, field text not null, field2 text not null );");
        fillDb(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
    //not relevant here so skip it
    }

    private void fillDb(SQLiteDatabase db)
    {
        InputStream rawInput = this.ctx.getResources().openRawResource(R.raw.data);

        SAXParserFactory factory = SAXParserFactory.newInstance();
        try
        {
            SAXParser saxParser = factory.newSAXParser();
            saxParser.parse(rawInput, new HandlerXmlData());
        }
        catch(Exception e)
        {
        //not relevant here so skip it              
        }


     //this manual entries would work here
        //ContentValues values = new ContentValues();
        //values.put("field", "aaa");
        //values.put("field2", "bbb");
        //db.insert("mytable", null, values);
                }

The problem is now that within my SAXParser

public class HandlerXmlData extends DefaultHandler
{

private String currentValue = "";

public void startDocument()
{

}

public void startElement(String namespaceURI, String localName, String qName, Attributes atts) 
{
}

public void characters(char[] ch, int start, int length)
{
    currentValue = new String(ch, start, length);
    //here I get the value from my XML
              //so I need something like db.insert(...)
}

public void endElement(String uri, String localName, String qName)
{

}

So within my SAXParser I cannot access the db object I have in my DatabaseHelper class. how do I bring these together?

Thanks, A.

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

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

发布评论

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

评论(2

蓝梦月影 2024-10-28 15:18:14

您可以通过构造函数将 db 对象传递给解析器吗?

public HandlerXmlData(SQLiteDatabase db){
    this.db = db;
}

Can you pass the db object to the parser through constructor?

public HandlerXmlData(SQLiteDatabase db){
    this.db = db;
}
墟烟 2024-10-28 15:18:14

您有几种选择来实现您想要做的事情。

  1. 您可以简单地将 SQLiteDatabase 对象传递到 HandlerXmlData 对象中,将该对象保留为实例变量,并在接近您的值时插入到其中。

  2. 您还可以创建一个代表您的数据库模型的 bean。在 HandlerXmlData 对象中,您可以有一个实例变量来存储此 bean 的集合,并在收到 sax 事件时将其添加到其中。解析完成后,您应该有一个方法,您可以通过该方法访问填充的模型,对其进行迭代并将每个条目插入到数据库中。

You have a couple options to achieve what you are trying to do.

  1. You could simply pass in the SQLiteDatabase object into your HandlerXmlData object, keep the object as an instance variable, and insert into it as you approach your values.

  2. You could also create a bean representing your database model. Within your HandlerXmlData object, you could have an instance variable storing a collection of this bean, and add to it as you receive the sax events. When the parsing is done, you should have a method from which you can access the populated model, iterate over it and insert each entry into your database.

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