Parsing Comma-Separated Values
By Doug Tidwell2005-05-18
Parsing the comma-separated value (CSV) file
After I had the data file, I started looking around for some code to parse comma-separated values. I didn't find exactly what I wanted, but I did stumble across the Java class StreamTokenizer. This class lets you design a rudimentary parser fairly easily. You select the delimiter between tokens, and it parses the file, converts data to strings and integers, and does some other nice things. You can view the code I wrote or download) it.
Color-coding our colorful coding
This article features colorized code listings, something we're experimenting with here at dW. To generate our color-coded listings, I'm using a couple of open-source tools. First, I load the document (Java, HTML, XML, whatever) into Emacs. Emacs defines colors for keywords, comments, function names, and other programming language constructs -- about a dozen in all. After Emacs has loaded and colored a file, I use the HTMLize package, an open-source utility written in the ever-popular Emacs Lisp language. HTMLize takes a listing exactly as it appears in Emacs, then converts it to HTML. The result is a fully color-coded file that highlights keywords, comments, function names, and so on.
Let us know what you think about these new and improved code listings.
If you'd like to do this kind of thing yourself, see Resources for the appropriate links.
To use this file, I typed:
|
That command opens and parses the CSV file and then converts it to XML. The XML for one employee looks like Listing 1. (You can also download output.xml.)
Listing 1. The XML output for one employee record from the CSV data sample
|
When I started working on this, I thought the first line of the CSV file would contain the column names from DB2. I was going to use those names as the XML tag names. I didn't immediately find a way to get DB2 to export data in this format, so I just made up the column names, using the imaginative naming scheme you see in Listing 1.
Tutorial Pages:
» Getting the data
» Parsing the comma-separated value (CSV) file
» Converting the generated XML
» Resources
First published by IBM DeveloperWorks
