CSV TRANSFORMATION

Description

Csvt is csv transformer task that allow to:

Csvt task parameters


|Attribute  | Description           | Value                         | Required  |
|-----------|-----------------------|-------------------------------|-----------|
|file       | input file            | <filename>.csv                | yes       |
|tofile     | output file           | <filename>.<mime>             | yes       |
|template   | freemarker template   | <filename>.ftl                | yes       |
|delimiter  | csv delimiter         | ; by default                  | no        |
|overwrite  | overwrite output file | true by default               | no        |
                  

Example

Transform csv datas to xml.

input.csv

  • input file

class;property;type
MyTodolist;id;integer
MyTodolist;label;string
MyTask;id;integer
MyTask;label;string
MyTask;listId;integer
                    

build.xml

  • ant file:

<project name="bidji-csvt" xmlns:bj="antlib:org.bidji.taskdefs">
<target name="csv2xml" description=" transform csv datas to xml">       
    <bj:csvt file="input.csv" tofile="output.xml" template="csv2xml.ftl" delimiter=";" overwrite="true"/>
</target>
</project>
                    

csv2xml.ftl

  • freemarker file

<module>
[#assign currentClass='']
[#foreach line in lines]
  [#if currentClass != line.class]
    [#if currentClass != '']
    </class>
    [/#if]
    <class name="${line.class}">
    [#assign currentClass=line.class]
  [/#if]
        <property name="${line.property}" type="${line.type}">  
  [#if !line?has_next]
    </class>
  [/#if]
[/#foreach]
</module>
                    

output.xml

  • result file

<module>
    <class name="MyTodolist">
        <property name="id" type="integer">
        <property name="label" type="string">
    </class>
    <class name="MyTask">
        <property name="id" type="integer">
        <property name="label" type="string">
        <property name="listId" type="integer">
    </class>
</module>