Java lib or application to transform CSV to XML documents?

Exists an existing application or collection in Java which will permit me to transform a CSV information documents to XML documents?

The XML tags would certainly be given via perhaps the first row having column headings.

2019-05-06 23:45:51
Source Share
Answers: 6

I recognize you requested for Java, yet this strikes me as a job well matched to a scripting language. Below is a fast (really straightforward) remedy created in Groovy.


hello world,1.0,3.3,4
goodbye world,1e9,-3.3,45
hello again,-1,23.33,456
hello world 3,1.40,34.83,4999
hello 2 world,9981.05,43.33,444


#!/usr/bin/env groovy

def csvdata = []
new File("test.csv").eachLine { line ->
    csvdata << line.split(',')

def headers = csvdata[0]
def dataRows = csvdata[1..-1]

def xml = new groovy.xml.MarkupBuilder()

// write 'root' element
xml.root {
    dataRows.eachWithIndex { dataRow, index ->
        // write 'entry' element with 'id' attribute
        entry(id:index+1) {
            headers.eachWithIndex { heading, i ->
                // write each heading with associated content

Writes the adhering to XML to stdout:

  <entry id='1'>
    <string>hello world</string>
  <entry id='2'>
    <string>goodbye world</string>
  <entry id='3'>
    <string>hello again</string>
  <entry id='4'>
    <string>hello world 3</string>
  <entry id='5'>
    <string>hello 2 world</string>

However, the code does really straightforward parsing (not thinking about priced estimate or run away commas) and also it does not make up feasible lacking information.

2019-05-19 15:36:28

As much as I recognize, there's no all set - made collection to do this for you, yet generating a device with the ability of converting from CSV to XML need to just need you to write an unrefined CSV parser and also link JDOM (or your XML Java collection of selection) with some adhesive code.

2019-05-09 08:13:41

I do not recognize why you would certainly intend to do this. It appears virtually like freight cult coding.

Transforming a CSV documents to XML does not add any kind of value. Your program is currently reviewing the CSV documents, so saying that you require XML does not function.

On the various other hand, reviewing the CSV documents, doing something with the values, and afterwards serializing to XML does make good sense (well, as high as making use of XML can make good sense ... ;)) yet you would apparently currently have a method of serializing to XML.

2019-05-08 19:56:25

Maybe this could aid : JSefa

You can read CSV file with this device and also serialize it to XML.

2019-05-08 19:48:49

This might be also standard or minimal of a remedy, yet could not you do a String.split() on each line of the documents, bearing in mind the outcome array of the first line to create the XML, and also simply spew each line's array information out with the correct XML components cushioning each model of a loop?

2019-05-08 19:48:04

There is absolutely nothing I recognize of that can do this without you at the very least creating a little of code ... You will certainly require 2 different collection :

  • A CSV Parser Framework
  • An XML Serialization Framework

The CSV parser I would certainly advise (unless you intend to have a little of enjoyable to write your very own CSV Parser ) is OpenCSV (A SourceForge Project for analyzing CSV Data )

The XML Serialization Framework need to be something that can scale in instance you intend to change huge (or massive ) CSV documents to XML : My referral is the Sun Java Streaming XML Parser Framework (See here ) which permits pull-parsing AND serialization.

2019-05-07 16:53:06