flat file generation tutorial
for generating files from a configuration file, Benerator can be run from a setup file. See the following example:
<?xml version="1.0" encoding="iso-8859-1"?> <import defaults="true" <generate type="product" count="3"> <consumer class="FlatFileEntityExporter"> </generate> |
The file has a central create-entities element which tells benerator to create three entities of type 'product'. An entity is the word, that benerator uses for business objects or, more general, composite data objects. Entities are composed of attributes .
The attributes are the data components of an entity. The attribute elements above describe, how the attributes should be generated:
- id is generated as incremental long values starting with 1
- ean_code is created by a distinct 'Generator' class. This is the first place where you can insert plugins. You will learn more about it later
- name is an arbitrary string of lenght 5
- price is a big decimal value between 0.49 and 99.99 (both inclusive), with a precision of 0.10. This means the values 0.49, 0.59, 0.69, ..., 99.79, 99.89, 99.99
The consumer element tells benerator to instantiate the JavaBean class FlatFileEntityExporter and have it process each generated entity. The entities will be written to a file 'products.flat' using a flat file format, rendering the columns with fixed with, alignment and padding character:
id[8r0] means that the attribute 'id' is padded to eight characters, aligned to the right and padded with '0' characters.
ean_code[13] means padding to 13 characters using default alignment (left) and character (' ')
(For a more detailed explanation of the file format, see file_format.html )
So, when running the example from the root directory of your benerator installation on a Unix system via
bin/benerator.sh demo/file/create_flat.ben.xml
or, on Windows, via
bin\benerator.bat demo\file\create_flat.ben.xml
benerator will create a flat file 'transactions.flat' like this:
| 00000001800035300638600009.850006 00000002800035000334000002.490018 00000003800035300638600009.850010 00000004807680000008500000.890022 00000005807680000008500000.890024 00000006807680000008500000.890024 ... |
(When running offline, you will need to remove the DOCTYPE declaration from the file demo/file/create_flat.ben.xml)


