Match CSV to model

I am writing a simple CSV processor in Java.

I am using JAXB to create a model in java from DTD. I need to process the CSV format in this model, and then translate it into XML, which corresponds to the DTD. I use JAXB to output data from a Java model to XML. I have to write the mapping of the CSV model myself.

At the moment, I can’t come up with a better solution than to directly correlate the CSV with the Java model by reading it and assigning it to the model in code.

Is there a more elegant solution for this that you can think of? Perhaps some library of multiple display, etc.?

Thanks in advance.

+4
source share
2 answers

I usually use the flatpack library to split CSV into Java models: Flatpack Project at sourceforge

This is a fairly simple use and use of XML mappings to handle the projection of a CSV model (thus, without causing a connection between your CSV and your Java objects)

+2
source

If you have direct mapping, you can use the thirdparty tool to directly bind csv to xml (e.g. csv2xml converter

Alternatively, read the csv file as a set of maps with a β€œ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 to java objects.

+1
source

All Articles