Jump to content
xisto Community
iGuest

Convert Csv To Xml The following options are supported

Recommended Posts

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.


Share this post


Link to post
Share on other sites

Hi, titoo.
Please have a more careful look at our forum rules.
One of these rule is usually named politeness, or scientifical honesty, or intellectual property respect.
Please feel free repeating what somebody else said.
But each sentence you did not write by yourself, you should mention the person who really said it, and put it between qutoes.
For instance, when repeating what I just said, you should write don the following way.
As Yordan said,

please be polite and honest

For instance, your post is very interesting, I am glad to be informed about that. But you should have mentionned that it is coming from the site https://www.progress.com/datadirect-connectors# Today I inserted the quotes for you in your post. Next time please use the "quote" button in order to insert the quotes by yourself. Else we could imagine that you are trying to cheat with our credit system, and this makes the admin's around here very angry. :)
Regards
Yordan

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.