Class FlatXmlDataSet

All Implemented Interfaces:
IDataSet, IDataSetConsumer

public class FlatXmlDataSet extends CachedDataSet
Reads and writes flat XML dataset document. Each XML element corresponds to a table row. Each XML element name corresponds to a table name. The XML attributes correspond to table columns.

Flat XML dataset document sample:

 <!DOCTYPE dataset SYSTEM "my-dataset.dtd">
 <dataset>
     <TEST_TABLE COL0="row 0 col 0"
         COL1="row 0 col 1"
         COL2="row 0 col 2"/>
     <TEST_TABLE COL1="row 1 col 1"/>
     <SECOND_TABLE COL0="row 0 col 0"
           COL1="row 0 col 1" />
     <EMPTY_TABLE/>
 </dataset>

To specify null values, omit corresponding attribute. In the above example, missing COL0 and COL2 attributes of TEST_TABLE second row represents null values.

Table metadata is deduced from the first row of each table by default. Beware that DbUnit may think a table misses some columns if the first row of that table has one or more null values. You can do one of the following things to avoid this:

  • Use a DTD. DbUnit will use the columns declared in the DTD as table metadata. DbUnit only supports external system URI. The URI can be absolute or relative.
  • Since DBUnit 2.3.0 there is a functionality called "column sensing" which basically reads in the whole XML into a buffer and dynamically adds new columns as they appear. It can be used as demonstrated in the following example:
       // since dbunit 2.4.7
       FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
       builder.setInputSource(new File("src/xml/flatXmlTableTest.xml"));
       builder.setColumnSensing(true);
       IDataSet dataSet = builder.build();
       
       // or dbunit release invalid input: '<'= 2.4.6:
       boolean enableColumnSensing = true;
       IDataSet dataSet = new FlatXmlDataSet(
                new File("src/xml/flatXmlTableTest.xml"), false, enableColumnSensing);
     

Since:
1.0 (Mar 12, 2002)
Version:
$Revision$ $Date$
Author:
Manuel Laflamme, gommma (gommma AT users.sourceforge.net), Last changed by: $Author$