views:

670

answers:

2

I am writing a simple CSV to XML processor in Java.

I am using JAXB to generate a model in java from a DTD. I need to process the CSV format into this model, and then marshall it into the XML that complies to the DTD. I am using JAXB to marhall the data from the Java model to the XML. I have to write the CSV-Model mapping myself.

At the moment, I can think of no better solution than to straight map the CSV to the Java model by reading it in and assigning it to the model in code.

Is there a more elegant solution to this that you can think of? Perhaps some reusable mapping library etc?

Thanks in advance.

+1  A: 

If you have a straightforard mapping, you could use a thirdparty tool to directly map csv to xml (e.g. csv2xml converter

Alternatively, read the csv file as a collection of maps with the 'key' as the name of the corresponding property in the java class. Then you can write a simple parser that will use reflection to set the csv values from the map into java objects.

Rahul
+1  A: 

Hi,

I usually use the flatpack library to parse CSVs into Java Models : Flatpack Project on sourceforge

It is quite simple to use and use XML mappings to handle the CSV to Model projection (thus not inducing thight coupling between your csv and your java objects)

WiseTechi
Awesome. This is just what I was looking for. Thanks.
bowsie