Wednesday, December 21, 2011

Creating a csv file from xml using groovy

Following up on the last article where I backed up the bank cds, I wanted to be able to query all of the cds at once to see if a check had cleared. In order to accomplish this, I mounted one of the backed up iso files, explored and found that most of the months used an xml file to list the cleared checks. I pulled the xml file from each iso into a directory and then used the following groovy code to read the file. Why groovy?, I guess it was the flavor of the day

This accomplished reading the file but then I wanted to output to a csv file. I decided to use the opencsv java package. After adding the import to the groovy script for import au.com.bytecode.opencsv.*, I then wrote the following code

Unfortunately the script did not run as opencsv expects a Listof String[] and did not like the groovy lists that it was given. Here's the exception:
Caught: java.lang.ClassCastException: [Ljava.util.ArrayList; cannot be cast to java.util.List at CheckClearer$_run_closure2.doCall(CheckClearer.groovy:30) at CheckClearer.run(CheckClearer.groovy:30)
I explicitly had to create String[]s and then pass them to CSVWriter. Here's the final script:

3 comments:

Nakia Mosley said...

Well done, and thanks for sharing your development,
it works well enough. But I was able to use the code and open .csv file https://wikiext.com/csv only after I updated Windows,
the browser and loaded the universal file viewer. And I determined right way was causing for open a lot of
file extensions as well.

Aruna Ram said...

Hi! I was much time to spend your post, it is very impressed to me. so I am regularly following your post. I would like more ideas from your blog...
Primavera Training in Chennai
Primavera Course in Chennai
Corporate Training in Chennai
Embedded System Course Chennai
Oracle Training in Chennai
Tableau Training in Chennai
Spark Training in Chennai
Excel Training in Chennai
Power BI Training in Chennai

Vicky Ram said...

Thank you sharing this kind of noteworthy information. Nice Post.

imedicalassistants
Education