Xml Dataset

 

The following sample demonstrates how you can export one or many tables from a database to a flat XML dataset file using a tool provided by Testing Utils:

 

Class.forName("com.mysql.jdbc.Driver");
Connection jdbcConnection = DriverManager.getConnection(DB_URL, DB_USER, DB_PWD);
IDatabaseConnection connection = new MySqlConnection(jdbcConnection, "db_test");
String[] tableNames = {
    "table_name_1",
    "table_name_2",
    "table_name_3"
};
File output = new File("src/test/dbunit/BaseDataSetForTesting-load.xml");
DbUnitUtils.exportDataset(connection, tableNames, output, DatasetType.XML);

The sample code extract data from a MySQL database.

With Testing Utils you can obtain export in

  • DatasetType.XML: a very verbose XML format
  • DatasetType.FLATXML: standard DBUnit XML format. Each XML element corresponds to a table row. Each XML element name corresponds to a table name. The XML attributes correspond to table columns.
  • DatasetType.EXCEL: a MS Excel documents. Each sheet represents a table. The first row of a sheet defines the columns names and remaining rows contains the data.

For futher documentation about types, please refer to the DBUnit official site.

Project info & quick links