Android sqlite 和 SaxParser
此刻我有点困惑。我收到一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过构造函数将 db 对象传递给解析器吗?
Can you pass the db object to the parser through constructor?
您有几种选择来实现您想要做的事情。
您可以简单地将 SQLiteDatabase 对象传递到 HandlerXmlData 对象中,将该对象保留为实例变量,并在接近您的值时插入到其中。
您还可以创建一个代表您的数据库模型的 bean。在 HandlerXmlData 对象中,您可以有一个实例变量来存储此 bean 的集合,并在收到 sax 事件时将其添加到其中。解析完成后,您应该有一个方法,您可以通过该方法访问填充的模型,对其进行迭代并将每个条目插入到数据库中。
You have a couple options to achieve what you are trying to do.
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.
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.