Using tables with known structures, create a simple XML document using the contents of table with a known structure.
Our data is a table that contains information about projects. It has three fields: id, project name and a version. We want to create an XML document which conforms to a particular schema.
Our data table is in CSV format and looks like:
# Example table data. id,project,version apple,Appleseed,version-1 orange,OrangeBlossom,version-2
We will store this in a file called projects.csv.
Our velocity template looks like:
<!-- Example use of table data with Velocity --> <!-- Source table should have at least three columns with the names "id", "project", and "version" --> <Doc> <records>${table.records.size()}</records> <table> #foreach ($record in $table.records) <record> <name>$record.id</name> <project>$record.project</project> <version>$record.version</version> </record> #end </table> </Doc>
We will store this in a file called example-known.vm.
In the template we expect our table to be in a context called "table" (which is established on the command line - see below). We will output a count of the number of records in the table (${table.record.size()}) and for each record we will output the value for each field marked up with XML tags.
Running igpp.docgen with the command:
Instructs igpp.docgen to parse the file "projects.csv" and place the results in a context named "table". Since the extension on "projects.csv" is ".csv" igpp.docgen will parse the file as a set of comma separated values, creating a record for each non-commented line in the file.
Running this command will generate an XML document that looks like:
<?xml version="1.0" encoding="UTF-8"?> <Doc> <records>2</records> <table> <record> <id>apple</id> <project>Appleseed</project> <version>version-1</version> </record> <record> <id>orange</id> <project>OrangeBlossom</project> <version>version-2</version> </record> </table> </Doc>