QUOTE
The following options are supported in the DataDirect XML Converter CSV converter precisely because they are unspecified in the real world, and vary between applications.
Whether the first row of the CSV data contains the names of the fields, or they are known by the sending and receiving applications implicitly
How commas within values are handled in CSV files
How quotes within quoted values are handled in CSV files. Are they doubled to escape them, or is there a special escape character, such as a backslash?
Whether single or double quotes are used
Whether runs of consecutive but empty fields should be ignored
What encoding is used Windows1252, ISO-8859-1 or US-ASCII are the most common
What line ending is used CR/LF from Windows, LF from Unix/Linux, or CR from the Macintosh are often seen
TSV Files
A close cousin to CSV files are TSV, or Tab Separated Value files. These are a common export format from spreadsheets also. There is a separate DataDirect XML Converter for these, but it actually is just the CSV Converter in disguise, since either can be converted to the other by changing the definition of the separator character from comma to tab.
CSV Sample File
Use the following sample CSV file for use in trying out DataDirect XML Converters:
Title,Author,ISBN13,Pages
1984,George Orwell,978-0451524935,268
Animal Farm,George Orwell,978-0451526342,144
Brave New World,Aldous Huxley,978-0060929879,288
Fahrenheit 451,Ray Bradbury,978-0345342966,208
Jane Eyre,Charlotte Brontė,978-0142437209,532
Wuthering Heights,Emily Brontė,978-0141439556,416
Agnes Grey,Anne Brontė,978-1593083236,256
Walden,Henry David Thoreau,978-1420922615,156
Walden Two,B. F. Skinner,978-0872207783,301
"Eats, Shoots & Leaves",Lynne Truss,978-1592400874,209
CSV File Reading and Writing
Using the above file, a tiny Java, C# or VB.Net program can quickly transform the input CSV to XML, or convert XML to CSV. For example, the following Java program converts the above CSV sample file into XML and writes the output to the console.
import java.io.*;
import javax.xml.transform.stream.*;
import com.ddtek.xmlconverter.*;
public class CSVDemo {
public static void main(String[]args) throws Throwable {
ConverterFactory factory = new ConverterFactory();
ConvertToXML toXML = factory.newConvertToXML("converter:CSV:first=yes");
OutputStreamWriter w = new OutputStreamWriter(System.out, "US-ASCII");
toXML.convert(new StreamSource(args[0]), new StreamResult(w));
}
}
This would create the following output (with some parts removed).
<?xml version="1.0" encoding="US-ASCII"?>
<table>
<row>
<Title>1984</Title>
<Author>George Orwell</Author>
<ISBN13>978-0451524935</ISBN13>
<Pages>268</Pages>
</row>
...
<row>
<Title>Jane Eyre</Title>
<Author>Charlotte Brontė</Author>
<ISBN13>978-0142437209</ISBN13>
<Pages>532</Pages>
</row>
...
<row>
<Title>Eats, Shoots & Leaves</Title>
<Author>Lynne Truss</Author>
<ISBN13>978-1592400874</ISBN13>
<Pages>209</Pages>
</row>
</table>
XML to CSV
Going from XML to CSV is just as important, and the code is just as small. It takes no special schema, and it doesn't even take XQuery or XSLT. Anything immediately under the root element becomes the rows, and anything under that becomes the values between the commas. In the above example, switching to the ConvertFromXML class and writing the sample XML in would produce the initial CSV file.
Just like the EDI converters and other converters in the DataDirect XML Converters suite, CSV (and TSV) converters are fully bidirectional.
How To Use CSV Files
CSV files often are used as a bridge between older applications and newer ones. Having a tool that transparently reads and writes CSV format files in your toolbox can be very valuable. So whether you have to merge inventory data coming from an outside vendor into your database or publish a list of financial figures for your accountants, being able to bridge these two worlds is the purpose of the DataDirect XML Converters. You can get started with directly accessing CSV data from your Java or .NET applications by downloading a free trial of DataDirect XML Converters today.
Whether the first row of the CSV data contains the names of the fields, or they are known by the sending and receiving applications implicitly
How commas within values are handled in CSV files
How quotes within quoted values are handled in CSV files. Are they doubled to escape them, or is there a special escape character, such as a backslash?
Whether single or double quotes are used
Whether runs of consecutive but empty fields should be ignored
What encoding is used Windows1252, ISO-8859-1 or US-ASCII are the most common
What line ending is used CR/LF from Windows, LF from Unix/Linux, or CR from the Macintosh are often seen
TSV Files
A close cousin to CSV files are TSV, or Tab Separated Value files. These are a common export format from spreadsheets also. There is a separate DataDirect XML Converter for these, but it actually is just the CSV Converter in disguise, since either can be converted to the other by changing the definition of the separator character from comma to tab.
CSV Sample File
Use the following sample CSV file for use in trying out DataDirect XML Converters:
Title,Author,ISBN13,Pages
1984,George Orwell,978-0451524935,268
Animal Farm,George Orwell,978-0451526342,144
Brave New World,Aldous Huxley,978-0060929879,288
Fahrenheit 451,Ray Bradbury,978-0345342966,208
Jane Eyre,Charlotte Brontė,978-0142437209,532
Wuthering Heights,Emily Brontė,978-0141439556,416
Agnes Grey,Anne Brontė,978-1593083236,256
Walden,Henry David Thoreau,978-1420922615,156
Walden Two,B. F. Skinner,978-0872207783,301
"Eats, Shoots & Leaves",Lynne Truss,978-1592400874,209
CSV File Reading and Writing
Using the above file, a tiny Java, C# or VB.Net program can quickly transform the input CSV to XML, or convert XML to CSV. For example, the following Java program converts the above CSV sample file into XML and writes the output to the console.
import java.io.*;
import javax.xml.transform.stream.*;
import com.ddtek.xmlconverter.*;
public class CSVDemo {
public static void main(String[]args) throws Throwable {
ConverterFactory factory = new ConverterFactory();
ConvertToXML toXML = factory.newConvertToXML("converter:CSV:first=yes");
OutputStreamWriter w = new OutputStreamWriter(System.out, "US-ASCII");
toXML.convert(new StreamSource(args[0]), new StreamResult(w));
}
}
This would create the following output (with some parts removed).
<?xml version="1.0" encoding="US-ASCII"?>
<table>
<row>
<Title>1984</Title>
<Author>George Orwell</Author>
<ISBN13>978-0451524935</ISBN13>
<Pages>268</Pages>
</row>
...
<row>
<Title>Jane Eyre</Title>
<Author>Charlotte Brontė</Author>
<ISBN13>978-0142437209</ISBN13>
<Pages>532</Pages>
</row>
...
<row>
<Title>Eats, Shoots & Leaves</Title>
<Author>Lynne Truss</Author>
<ISBN13>978-1592400874</ISBN13>
<Pages>209</Pages>
</row>
</table>
XML to CSV
Going from XML to CSV is just as important, and the code is just as small. It takes no special schema, and it doesn't even take XQuery or XSLT. Anything immediately under the root element becomes the rows, and anything under that becomes the values between the commas. In the above example, switching to the ConvertFromXML class and writing the sample XML in would produce the initial CSV file.
Just like the EDI converters and other converters in the DataDirect XML Converters suite, CSV (and TSV) converters are fully bidirectional.
How To Use CSV Files
CSV files often are used as a bridge between older applications and newer ones. Having a tool that transparently reads and writes CSV format files in your toolbox can be very valuable. So whether you have to merge inventory data coming from an outside vendor into your database or publish a list of financial figures for your accountants, being able to bridge these two worlds is the purpose of the DataDirect XML Converters. You can get started with directly accessing CSV data from your Java or .NET applications by downloading a free trial of DataDirect XML Converters today.

