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.

0
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.

test.csv

string,float1,float2,integer
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

csvtoxml.groovy

#!/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
                "${heading}"(dataRow[i])
            }
        }
    }
}

Writes the adhering to XML to stdout:

<root>
  <entry id='1'>
    <string>hello world</string>
    <float1>1.0</float1>
    <float2>3.3</float2>
    <integer>4</integer>
  </entry>
  <entry id='2'>
    <string>goodbye world</string>
    <float1>1e9</float1>
    <float2>-3.3</float2>
    <integer>45</integer>
  </entry>
  <entry id='3'>
    <string>hello again</string>
    <float1>-1</float1>
    <float2>23.33</float2>
    <integer>456</integer>
  </entry>
  <entry id='4'>
    <string>hello world 3</string>
    <float1>1.40</float1>
    <float2>34.83</float2>
    <integer>4999</integer>
  </entry>
  <entry id='5'>
    <string>hello 2 world</string>
    <float1>9981.05</float1>
    <float2>43.33</float2>
    <integer>444</integer>
  </entry>
</root>

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.

0
2019-05-19 15:36:28
Source

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.

0
2019-05-09 08:13:41
Source

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.

0
2019-05-08 19:56:25
Source

Maybe this could aid : JSefa

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

0
2019-05-08 19:48:49
Source

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?

0
2019-05-08 19:48:04
Source

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.

0
2019-05-07 16:53:06
Source