Mapping CSV to a Model - java

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.

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)

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.

Related

Convert XML object to Json using velocity template

I have a use-case where input data is in xml format. It contains multiple fields where we are interested in reading only few fields.
Based on the different use-case, separate velocity templates would have to written to convert xml to json data.
I could not find a better guide on how to write velocity template to convert XML to json. Can someone please help us how to write velocity for conversion from XML to JSON.
Thanks in advance!
First convert your XML into a DTO, and then DTO into Json. You should split your architecture in more than just one layer. The data access layer will handle the WS call and the controller will know how to answer the REST call. Velocity is just a template engine, not a converting tool. You should check Jackson or Gson.

JSON To JSON Transformation | Template Engine for Java

I have a requirement to transform an incoming JSON to an output JSON. For this I am looking for a solution that can work based on templates. What I have in my mind is a solution on lines of XSLT transformation that allows converting an XML to a desired output format (XML, HTML, Text) defined by the style sheet.
One option(or rather a workaround) to use XSLT is to convert JSON to XML that is:
input JSON -> XML -> transform -> output JSON
This approach would have a performance overhead of converting JSON to XML and this would become prominent as the size of incoming object increases.
I found a Node/client layer solution that transforms JSON based on the rules specified in a template. More details about can be found [here][1]. However, I was not able to find any solution that works for a java based application.
Any thoughts/help in terms of solution/frameworks to resolve this would be really helpful.
Thanks.
You could try JOLT, advertised as a JSON to JSON transformation library written in Java.
Or you can search this thread for other libraries and tools which can transform JSON.
The new XSLT 3.0 draft also includes support for JSON as input and output format. Saxon has already started an implementation and seems to support for the JSON part.
You could try JSLT, which is a transform language where you write the fixed part of the output in JSON syntax, then insert expressions to compute the values you want to insert in the template. It's quite similar to how XSLT and XPath work together.
It's implemented in Java on top of Jackson.
For simple transformations you can use jmom library.
For a complex transformation you can use template framework like freemarker.
And convert json data to Map/List form using json library so it can be used by template framework.

Creating HTML from XML v/s JAVA object

I am trying to create HTML file from the result of an execution. The result is in the form of XML. There are few transformers that I can use to transform XML to HTML using XSLT file.
Other thing I will also have is the JAVA object of result which I can use for converting it to HTML.
Which of the above two approach is better and is there any API that I can use to convert java object to HTML other than XSLT or FILE I/O.
any one help?
I believe you have to go by xml (either directly or generated from your java object by jaxb).
In principle the templating frameworks (Velocity, Freemarker ...) can let you prepare a template into which you can inject your java object and render the response as you whish. But personally I think it will be easier/simple just to transform the xml that you already have

How to generate xml file by Java?

I have to generate huge and quite complex xml files by Java. I have to fetch the data from a Oracle database. What I really don't know is a proper and reliable way to this? I could of course create a String and concatenate all the tags, attributes and data but it doesn't feel right. I guess this is a quite common task and there are many established ways to this by Java. My question is what is the best way to this? What is your suggestion?
Thank you for any clues...
You could use JAXB for building XML out of structured objects that are a result of querying your data store.
If your object hierarchy is not complex, you can use Oracle's capability to generate results in XML.
There are several options for object to xml transformation.
jaxb
saxparser
dom parser
I would personally suggest JAXB for easy of use and saxparser for performance centric application.
You can use JAXP(Java API for XML parsing ) to create a XML structure.This is having all the features you wanted.

Generate and parse text files in Java

I'm looking for a library/framework to generate/parse TXT files from/into Java objects.
I'm thinking in something like Castor or JAXB, where the mapping between the file and the objects can be defined programmatically or with XML/annotations. The TXT file is not homogeneous and has no separators (fixed positions). The size of the file is not big, therefore DOM-like handling is allowed, no streaming required.
For instance:
TextWriter.write(Collection objects) -> FileOutputStream
TextReader.read(FileInputStream fis) -> Collection
I suggest you use google's protocol buffers
Protocol buffers are a flexible, efficient, automated mechanism for
serializing structured data – think XML, but smaller, faster, and
simpler. You define how you want your data to be structured once, then
you can use special generated source code to easily write and read
your structured data to and from a variety of data streams and using a
variety of languages. You can even update your data structure without
breaking deployed programs that are compiled against the "old" format.
Protobuf messages can be exported/read in binary or text format.
Other solutions would depend on what you call text file : if base64 is texty enough for you, you could simply use java standard serialization with base64 encoding of the binary stream.
You can do this using Jackson serialize to JSON and back
http://jackson.codehaus.org/
Just generate and parse it with XML or JSON formats, there's a whole load of libraries out there that will do all the work for you.

Categories

Resources