@Before There will be probably some duplicate questions suggestions, I don\'t think that is the case maybe read this first, I\'ll try to be as brief as possible. Title gives
The best way to code based on your described requirement is to use the easy feature of FreeMarker and XML processing. See the docs.
In this case you will only need the template that will produce a CSV.
An alternative to this is XMLGen, but very similar in approach. Just look at that diagram and examples, and instead of SQL statements, you will output CSV.
These two similar approaches are not "conventional" but do the job very quickly for your situation, and you don't have to learn XSL (quite hard to master I think).