|
|
see also at http://dbunit.sourceforge.net/faq.html#extractHow to extract a flat XML dataset from my database? The following sample demonstrates how you can export one or many tables from a database to an flat XML dataset file.
public class DatabaseExportSample { public static void main(String[] args) throws Exception { // database connection Class driverClass = Class.forName("org.hsqldb.jdbcDriver"); Connection jdbcConnection = DriverManager.getConnection( "jdbc:hsqldb:sample", "sa", ""); IDatabaseConnection connection = new DatabaseConnection(jdbcConnection); // partial database export QueryDataSet partialDataSet = new QueryDataSet(connection); partialDataSet.addTable("FOO", "SELECT * FROM TABLE WHERE COL='VALUE'"); partialDataSet.addTable("BAR"); FlatXmlDataSet.write(partialDataSet, new FileOutputStream("partial.xml")); // full database export IDataSet fullDataSet = connection.createDataSet(); FlatXmlDataSet.write(fullDataSet, new FileOutputStream("full.xml")); // dependent tables database export: export table X and all tables that // have a PK which is a FK on X, in the right order for insertion String[] depTableNames = TablesDependencyHelper.getAllDependentTables( connection, "X" ); IDataSet depDataset = connection.createDataSet( depTableNames ); FlatXmlDataSet.write(depDataSet, new FileOutputStream("dependents.xml")); } }<div class="shareblock">Share this post: [email=?body=Thought you might like this: http://www.cactus.org.cn/archive/2007/09/11/dbunit-extract-a-flat-xml-dataset-from-database.aspx&;subject=%5bDBUnit%5dextract+a+flat+XML+dataset+from+database]E-Mail it![/email] | Bookmark it! | Digg it! | Reddit! | Kick it! | Live it! |
|