-1

I'm pretty new to CSV and XML files in general, and I want to convert every CSV file in my folder to an XML file. I'd like some tips how to work with CSV files, how to go about processing and converting the files, and how to go about repeating the conversion process for every file in my folder. Thanks in advance.

Example CSV

id,name,surname,street,number,town
112345,gentoo,king,bourkley,6,novi sad
446636,mark,smith,milson,14,capetown

Example XML

<root>
  <entry id='1'>
    <id>112345</id>
    <name>gentoo</name>
    <surname>king</surname>
    <street>bourkley</street>
    <number>6</number>
    <town>novi sad</town>
  </entry>
  <entry id='2'>
    <id>112345</id>
    <name>mark</name>
    <surname>smith</surname>
    <street>milson</street>
    <number>14</number>
    <town>capetown</town>
  </entry>
</root>

Of course, I'd like the simplest possible solution. Using libraries as basic as they get.

Gentoo King
  • 35
  • 1
  • 2
  • 7

2 Answers2

1

CSV Parsing: Apache Commons CSV

Creating an XML file: Tutorial

Finding all files of a specific type in a directory: SO Answer

Put these together and VOILA!

Community
  • 1
  • 1
nattyddubbs
  • 2,085
  • 15
  • 24
0

I would suggest pulling in a csv parser, like opencsv to read the csv file into a bean. Then use a library like jaxb to serialize the bean to XML.

Instructions for parsing CSV with opencsv: http://opencsv.sourceforge.net/#how-to-read

Tutorial for jaxb: http://www.mkyong.com/java/jaxb-hello-world-example/

jgitter
  • 3,396
  • 1
  • 19
  • 26