I am using SAX Parser to parse the XML file over the network.
While parsing I want to add data into database so for that I use INSERT query to add the data. But each insert takes: 10 – 15 ms and I have almost 150 records which almost takes 130 seconds to parse and insert.
I have tried to wrap around the insert query in transactions but it still gives me same time. I have attached my code. I am not sure if I am parsing it correctly or my insert transaction is wrong?
XMLHandler.java
public class XMLHandler extends DefaultHandler {
private static boolean inKey = false;
private static boolean inCode = false;
private static boolean inTitle = false;
private static boolean inType = false;
private static boolean inRoom = false;
private static boolean inDescription = false;
private static boolean inStart = false;
private List List = new List();
public void startElement(String uri, String name, String qName,
Attributes atts) {
if (name.trim().equals("key"))
inKey = true;
else if (name.trim().equals("code"))
inCode = true;
else if (name.trim().equals("title"))
inTitle = true;
else if (name.trim().equals("type"))
inType = true;
else if (name.trim().equals("room"))
inRoom = true;
else if (name.trim().equals("description"))
inDescription = true;
else if (name.trim().equals("start"))
inClassStart = true;
}
public void endElement(String uri, String name, String qName)
throws SAXException {
if (name.trim().equals("key"))
inKey = false;
else if (name.trim().equals("code"))
inCode = false;
else if (name.trim().equals("title"))
inTitle = false;
else if (name.trim().equals("type"))
inType = false;
else if (name.trim().equals("room"))
inRoom = false;
else if (name.trim().equals("description"))
inDescription = false;
else if (name.trim().equals("start"))
inClassStart = false;
}
public void characters(char ch[], int start, int length) {
String chars = (new String(ch).substring(start, start + length));
try {
if(inKey)
List.key = chars;
if(inCode)
List.code = chars;
if(inTitle)
List.title = chars;
if(inType)
List.type = chars;
if(inRoom)
List.room = chars;
if(inDescription)
List.description = chars;
if(inStart)
List.start = chars;
DB.insertFeed(List.key, List.code, List.title, List.type, List.room, List.description, List.start);
} catch (Exception e) {
Log.e("NewsDroid", e.toString());
}
}
DatabaseManager.java
public void insertFeed( String key, String code, String title, String type, String room, String desc,String start) {
db.beginTransaction();
try{
String sql = "INSERT OR REPLACE INTO " + TEST+ "(KEY,CODE,TITLE, TYPE ,ROOM , DESCRIPTION, START) VALUES" + "(?,?,?,?,?,?,?);";
Object [] bindArgs = new Object[]{key,code,title,type,room, desc,start};
db.execSQL(sql, bindArgs);
db.setTransactionSuccessful();
}
catch (SQLException e){}
finally{
db.endTransaction();
}
}
You are inserting one row each time and might be doing db.open() and db.close() each time.
Instead of this try to use the “InsertHelper”.
A crude example to use it shown below:
private void insertTweetSourcesInBulk(ArrayList tweetSources, boolean replace) {InsertHelper ih = new InsertHelper(db, TABLE_NAME_TWEET_SOURCE);