Exporting Database File As Text Or Xml In Android
Hi I have been looking for days on how to export my database as a xml or text file but cant seem to find what I am looking for. All the tutorials and code snipets I have come acros
Solution 1:
Check following
http://mgmblog.com/2009/02/06/export-an-android-sqlite-db-to-an-xml-file-on-the-sd-card/
Export sqlite database file into XML and then into Excel spreadsheet
Exporting a SQLite database to an XML file in Android
Solution 2:
publicclassDataXmlExporterextendsDataBaseDemoActivity {
staticfinalStringDATASUBDIRECTORY="bookwormdata";
// private final SQLiteDatabase db;private XmlBuilder xmlBuilder;
Button butt;
/* public DataXmlExporter(final SQLiteDatabase db) {
this.db = db;
}*/publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main7);
butt=(Button)findViewById(R.id.exportxml);
String exportFileNamePrefix=exportxml;
// public void export(final String dbName, final String exportFileNamePrefix) throws IOException {
Log.i(Constants._COUNT, "exporting database - " + dbname + " exportFileNamePrefix=" + exportFileNamePrefix);
try {
xmlBuilder = newXmlBuilder();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
xmlBuilder.start(dbname);
// get the tablesStringsql="select * from dept";
Cursorc= db.rawQuery(sql, newString[0]);
if (c.moveToFirst()) {
//while (c.moveToNext()) {StringtableName="dept";//c.getString(c.getColumnIndex("dep_id"));// skip metadata, sequence, and uidx (unique indexes)if (!tableName.equals("android_metadata") && !tableName.equals("sqlite_sequence")
&& !tableName.startsWith("uidx")) {
try {
exportTable(tableName);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// }
}
StringxmlString=null;
try {
xmlString = xmlBuilder.end();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
writeToFile(xmlString, exportFileNamePrefix + ".xml");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i(Constants._COUNT, "exporting database complete");
Toast.makeText(DataXmlExporter.this, "DB xml backup Successfully", 2000).show();
butt.setOnClickListener(newView.OnClickListener() {
publicvoidonClick(View v) {
// TODO Auto-generated method stubIntentintent=newIntent(DataXmlExporter.this, DataBaseDemoActivity.class);
startActivity(intent);
}
});
}
privatevoidexportTable(final String tableName)throws IOException {
xmlBuilder.openTable(tableName);
Stringsql="select * from " + tableName;
Cursorc= db.rawQuery(sql, newString[0]);
if (c.moveToFirst()) {
intcols= c.getColumnCount();
do {
xmlBuilder.openRow();
for (inti=0; i < cols; i++) {
/*if(i==6)
{
//String id = c.getString( c.getColumnIndex("photo"));
String str = new String(image);
xmlBuilder.addColumn(c.getColumnName(i), str);
}*/
xmlBuilder.addColumn(c.getColumnName(i), c.getString(i));
}
xmlBuilder.closeRow();
} while (c.moveToNext());
}
c.close();
xmlBuilder.closeTable();
}
privatevoidwriteToFile(final String xmlString, final String exportFileName)throws IOException {
Filedir=newFile(Environment.getExternalStorageDirectory(), DataXmlExporter.DATASUBDIRECTORY);
if (!dir.exists()) {
dir.mkdirs();
}
Filefile=newFile(dir, exportFileName);
file.createNewFile();
ByteBufferbuff= ByteBuffer.wrap(xmlString.getBytes());
FileChannelchannel=newFileOutputStream(file).getChannel();
try {
channel.write(buff);
} finally {
if (channel != null) {
channel.close();
}
}
}
/**
* XmlBuilder is used to write XML tags (open and close, and a few attributes)
* to a StringBuilder. Here we have nothing to do with IO or SQL, just a fancy StringBuilder.
*
* @author ccollins
*
*/staticclassXmlBuilder {
privatestaticfinalStringOPEN_XML_STANZA="<?xml version=\"1.0\" encoding=\"utf-8\"?>";
privatestaticfinalStringCLOSE_WITH_TICK="'>";
privatestaticfinalStringDB_OPEN="<database name='";
privatestaticfinalStringDB_CLOSE="</database>";
privatestaticfinalStringTABLE_OPEN="<table name='";
privatestaticfinalStringTABLE_CLOSE="</table>";
privatestaticfinalStringROW_OPEN="<row>";
privatestaticfinalStringROW_CLOSE="</row>";
privatestaticfinalStringCOL_OPEN="<col name='";
privatestaticfinalStringCOL_CLOSE="</col>";
privatefinal StringBuilder sb;
publicXmlBuilder()throws IOException {
sb = newStringBuilder();
}
voidstart(final String dbName) {
sb.append(XmlBuilder.OPEN_XML_STANZA);
sb.append(XmlBuilder.DB_OPEN + dbName + XmlBuilder.CLOSE_WITH_TICK);
}
String end()throws IOException {
sb.append(XmlBuilder.DB_CLOSE);
return sb.toString();
}
voidopenTable(final String tableName) {
sb.append(XmlBuilder.TABLE_OPEN + tableName + XmlBuilder.CLOSE_WITH_TICK);
}
voidcloseTable() {
sb.append(XmlBuilder.TABLE_CLOSE);
}
voidopenRow() {
sb.append(XmlBuilder.ROW_OPEN);
}
voidcloseRow() {
sb.append(XmlBuilder.ROW_CLOSE);
}
voidaddColumn(final String name, final String val)throws IOException {
sb.append(XmlBuilder.COL_OPEN + name + XmlBuilder.CLOSE_WITH_TICK + val + XmlBuilder.COL_CLOSE);
}
}
}
Try this. It's work for me.
Post a Comment for "Exporting Database File As Text Or Xml In Android"