I’m currently working on refactoring an UI/ORM application and the database it connects to. I need to change a lot of things in the db design so I was looking for a simple java tool that would allow me to easily export tables from the database to a defined XML format.
A tool that would produce something like that:
value_col1 value_val2 value_col1 value_val2
I briefly googled but I couldn’t find anything free and easy to use. I know there must be! Anyway, I found an elegant way to do it with Groovy and wanted to share it.
def sql = groovy.sql.Sql.newInstance("connectionString", "user", "password", "db_driver")
def fileOut = new FileWriter("/path/to/out/put/file.xml")
def xml = new groovy.xml.MarkupBuilder(fileOut)
xml.table_entries {
sql.eachRow("select * from schema.table") {row ->
entry {
column1(row.column1)
column2(row.column2)
...
}
}
}
1 minute to write the code to export one table, isn’t Groovy great?