Regardless of language, if you don't want to hard code the values, then one will need some sort of meta data that describes how the data will be mapped, formatted, and output.
In this example, it could be called a mapper or formatter.
Map/format files (XML, JSON, or simliar) would describe how the data would be formatted and written. Your application would read in the map/format file and use it to create the output.
Then your application could format the CSV anyway you see fit, without a programming change. One could also extend this to flat or fixed formats and XML as well. Then your code is generic as it uses the mapping meta data to create the CSV file.
For the CSV case, at a high level one would need to describe:
- Name and Order of fields
- Delimiter (Sometimes comma is not used)
- Whether or not to include a header
- Whether or not to include quotes around the data
Just as a side note, there is a time difference to develop the two applications. Hardcoded field values are much faster to develop, although as you have pointed out less elegant. But if you need to get something done quickly, the aproach is OK.
Developing something more generic would take more time up front, but if your producing a lot of CSV formatted files of different format, in the long run one would get ROI on it.
With this approach, one can also write some sort of nice "GUI" for business analysts to use to create the map files so developers will be less involved in the overall process.
It is hard to understand your exact question but generally speaking JSON would be a good choice to integrate with the client side in Java. You can serialize POJOs using jackson. But better yet you can deserialize proto buffers to JSON.
In terms of a framework for Java, I would recommend Spring MVC. You need an instance of an application server (tomcat, jetty etc.) i.e. a jvm in which to run. It has great RESTful support and provides to and fro conversion from POJO's to JSON by simply including jackson in the classpath.
Best Answer
May this link help you
in this code
XStream api
is used which is used to convert XML to Object and vice versa.