Search This Site
Dec 01 2008 18:42 UTC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
XML ManagerStandard Edition |
• Try the Demo! • Download the FREE Trial! • Product Home Page |
• Getting Started Guide • How You Can Save Money • All Your Purchase Options |
Learn how to load CSV data into your program, and output it as HTML:
To compile and run the Java programs in this example, either import them into your IDE
(say, Eclipse or NetBeans), or follow
the standard manual compilation instructions.
When running the programs, run them in the same folder as this README.htm file, so that
they can find their input files.
Let's use a list of Shakespeare's plays as our example data. The CSV file contains the list of all of Shakespeare's known plays, with data fields for title, type of play (comedy, tragedy, history), and year of writing.
plays.csvTitle, Type, Year "A Midsummer Night's Dream", COMEDY, 1595 "All's Well That Ends Well", COMEDY, 1602 "Antony and Cleopatra", TRAGEDY, 1606 "As You Like It", COMEDY, 1599 "Comedy of Errors", COMEDY, 1589 "Coriolanus", TRAGEDY, 1607 "Cymbeline", HISTORY, 1609 "Hamlet", TRAGEDY, 1600 "Henry IV, Part I", HISTORY, 1597 "Henry IV, Part II", HISTORY, 1597 "Henry V", HISTORY, 1598 "Henry VI, Part I", HISTORY, 1591 "Henry VI, Part II", HISTORY, 1590 "Henry VI, Part III", HISTORY, 1590 "Henry VIII", HISTORY, 1612 "Julius Caesar", TRAGEDY, 1599 "King John", HISTORY, 1596 "King Lear", TRAGEDY, 1605 "Love's Labour's Lost", COMEDY, 1594 "Macbeth", TRAGEDY, 1605 "Measure for Measure", COMEDY, 1604 "Much Ado about Nothing", COMEDY, 1598 "Othello", TRAGEDY, 1604 "Pericles", HISTORY, 1608 "Richard II", HISTORY, 1595 "Richard III", HISTORY, 1592 "Romeo and Juliet", TRAGEDY, 1594 "The Merchant of Venice", COMEDY, 1596 "The Merry Wives of Windsor", COMEDY, 1600 "The Taming of the Shrew", COMEDY, 1593 "The Tempest", COMEDY, 1611 "The Winter's Tale", COMEDY, 1610 "Timon of Athens", TRAGEDY, 1607 "Titus Andronicus", TRAGEDY, 1593 "Troilus and Cressida", TRAGEDY, 1601 "Twelfth Night", COMEDY, 1599 "Two Gentlemen of Verona", COMEDY, 1594
We want to be able to load this data into our program, so that we can convert the data into HTML that we can display nicely.
First, let's look at loading the data. It's not a very big file, so let's just load all the CSV data into memory at once.
We'll use the simplest method in CSV Manager: load. We can pass in a File object and CSV Manager will load the file,
converting the CSV text data into a List of String[] arrays.
Here's the code:
CsvManager csvman = new CsvManager(); List data = csvman.load( "plays.csv" );
(The full code for this example is shown in the LoadPlays.java file)
That's it. All done. Now you have a nice List of String[] arrays to work with. You can loop through all the
elements of the List, cast each into a String[] array, and then access the elements of the String[] array
one by one. You'll have one String[] array for each line of the CSV file.
How many elements will be in the String[] arrays? As many data fields are there are in each line of the CSV file —
CSV Manager never drops data. You always get all the data that's there, even if some lines in the CSV file are longer than others.
You can find out more about how CSV Manager handles missing data in the documentation for the CsvSpec.setNumFields method.
Now that we have our CSV data in a convenient data structure (a List of String[] arrays), we can just loop
through the data and keep adding HTML elements to a StringBuffer.
Here's the code:
StringBuffer htmlTable
= new StringBuffer("<html><body><table>\n");
for( int line = 0; line < data.size(); line++ ) {
htmlTable.append( "<tr>\n" );
String[] fields = (String[]) data.get(line);
for( int field = 0; field < fields.length; field++ ) {
String celltype = (0==line?"th":"td");
htmlTable.append( "<"+celltype+" align=\"left\">"
+fields[field]
+"</"+celltype+">\n" );
}
htmlTable.append( "</tr>\n" );
}
htmlTable.append( "</table></body></html>\n" );
We go through the data one line at a time, and for each line, we go through the line one data field at a time.
For each line we need a table row: <tr> ... </tr>. And for each field we need a table cell:
<td> ... </td>. Inside the table cell we put the String contents of the CSV data field,
taken from the String[] provided by CSV Manager for that line of the CSV file.
All this HTML text is built up in the StringBuffer. We also add the surrounding HTML elements for a normal HTML page, and
the HTML table: <html><body><table> ... </table></body></html>
We've left the head element out as we don't need it for this example.
One other thing that we're doing is making sure that the headers of the CSV file, that is, the fields of the first row, appear
as table headers in the HTML. For this special case, we output <th> ... </th> for each data field.
This only applies to the first line of data (when line == 0).
At the end of all this we have our HTML page text in the htmlTable variable. Now all we have to do is save this HTML
to a file and we're done!
We'll just save the HTML text using some standard Java classes, so here's the code:
FileWriter htmlFile = new FileWriter( "plays.htm" ); htmlFile.write( htmlTable.toString() ); htmlFile.close();
These lines of code create the file plays.htm and save the HTML text to it.
If you compile and run the LoadPlays.java program, you'll find the plays.htm file
output beside the plays.csv file.
Here's the output HTML, so you can see what we've actually created:
<html><body><table> <tr> <th align="left">Title</th> <th align="left">Type</th> <th align="left">Year</th> </tr> <tr> <td align="left">A Midsummer Night's Dream</td> <td align="left">COMEDY</td> <td align="left">1595</td> </tr> <tr> <td align="left">All's Well That Ends Well</td> <td align="left">COMEDY</td> <td align="left">1602</td> </tr> <tr> <td align="left">Antony and Cleopatra</td> <td align="left">TRAGEDY</td> <td align="left">1606</td> </tr> <tr> <td align="left">As You Like It</td> <td align="left">COMEDY</td> <td align="left">1599</td> </tr> <tr> <td align="left">Comedy of Errors</td> <td align="left">COMEDY</td> <td align="left">1589</td> </tr> <tr> <td align="left">Coriolanus</td> <td align="left">TRAGEDY</td> <td align="left">1607</td> </tr> <tr> <td align="left">Cymbeline</td> <td align="left">HISTORY</td> <td align="left">1609</td> </tr> <tr> <td align="left">Hamlet</td> <td align="left">TRAGEDY</td> <td align="left">1600</td> </tr> <tr> <td align="left">Henry IV, Part I</td> <td align="left">HISTORY</td> <td align="left">1597</td> </tr> <tr> <td align="left">Henry IV, Part II</td> <td align="left">HISTORY</td> <td align="left">1597</td> </tr> <tr> <td align="left">Henry V</td> <td align="left">HISTORY</td> <td align="left">1598</td> </tr> <tr> <td align="left">Henry VI, Part I</td> <td align="left">HISTORY</td> <td align="left">1591</td> </tr> <tr> <td align="left">Henry VI, Part II</td> <td align="left">HISTORY</td> <td align="left">1590</td> </tr> <tr> <td align="left">Henry VI, Part III</td> <td align="left">HISTORY</td> <td align="left">1590</td> </tr> <tr> <td align="left">Henry VIII</td> <td align="left">HISTORY</td> <td align="left">1612</td> </tr> <tr> <td align="left">Julius Caesar</td> <td align="left">TRAGEDY</td> <td align="left">1599</td> </tr> <tr> <td align="left">King John</td> <td align="left">HISTORY</td> <td align="left">1596</td> </tr> <tr> <td align="left">King Lear</td> <td align="left">TRAGEDY</td> <td align="left">1605</td> </tr> <tr> <td align="left">Love's Labour's Lost</td> <td align="left">COMEDY</td> <td align="left">1594</td> </tr> <tr> <td align="left">Macbeth</td> <td align="left">TRAGEDY</td> <td align="left">1605</td> </tr> <tr> <td align="left">Measure for Measure</td> <td align="left">COMEDY</td> <td align="left">1604</td> </tr> <tr> <td align="left">Much Ado about Nothing</td> <td align="left">COMEDY</td> <td align="left">1598</td> </tr> <tr> <td align="left">Othello</td> <td align="left">TRAGEDY</td> <td align="left">1604</td> </tr> <tr> <td align="left">Pericles</td> <td align="left">HISTORY</td> <td align="left">1608</td> </tr> <tr> <td align="left">Richard II</td> <td align="left">HISTORY</td> <td align="left">1595</td> </tr> <tr> <td align="left">Richard III</td> <td align="left">HISTORY</td> <td align="left">1592</td> </tr> <tr> <td align="left">Romeo and Juliet</td> <td align="left">TRAGEDY</td> <td align="left">1594</td> </tr> <tr> <td align="left">The Merchant of Venice</td> <td align="left">COMEDY</td> <td align="left">1596</td> </tr> <tr> <td align="left">The Merry Wives of Windsor</td> <td align="left">COMEDY</td> <td align="left">1600</td> </tr> <tr> <td align="left">The Taming of the Shrew</td> <td align="left">COMEDY</td> <td align="left">1593</td> </tr> <tr> <td align="left">The Tempest</td> <td align="left">COMEDY</td> <td align="left">1611</td> </tr> <tr> <td align="left">The Winter's Tale</td> <td align="left">COMEDY</td> <td align="left">1610</td> </tr> <tr> <td align="left">Timon of Athens</td> <td align="left">TRAGEDY</td> <td align="left">1607</td> </tr> <tr> <td align="left">Titus Andronicus</td> <td align="left">TRAGEDY</td> <td align="left">1593</td> </tr> <tr> <td align="left">Troilus and Cressida</td> <td align="left">TRAGEDY</td> <td align="left">1601</td> </tr> <tr> <td align="left">Twelfth Night</td> <td align="left">COMEDY</td> <td align="left">1599</td> </tr> <tr> <td align="left">Two Gentlemen of Verona</td> <td align="left">COMEDY</td> <td align="left">1594</td> </tr> </table></body></html>
... which ends up looking like this:
| Title | Type | Year |
|---|---|---|
| A Midsummer Night's Dream | COMEDY | 1595 |
| All's Well That Ends Well | COMEDY | 1602 |
| Antony and Cleopatra | TRAGEDY | 1606 |
| As You Like It | COMEDY | 1599 |
| Comedy of Errors | COMEDY | 1589 |
| Coriolanus | TRAGEDY | 1607 |
| Cymbeline | HISTORY | 1609 |
| Hamlet | TRAGEDY | 1600 |
| Henry IV, Part I | HISTORY | 1597 |
| Henry IV, Part II | HISTORY | 1597 |
| Henry V | HISTORY | 1598 |
| Henry VI, Part I | HISTORY | 1591 |
| Henry VI, Part II | HISTORY | 1590 |
| Henry VI, Part III | HISTORY | 1590 |
| Henry VIII | HISTORY | 1612 |
| Julius Caesar | TRAGEDY | 1599 |
| King John | HISTORY | 1596 |
| King Lear | TRAGEDY | 1605 |
| Love's Labour's Lost | COMEDY | 1594 |
| Macbeth | TRAGEDY | 1605 |
| Measure for Measure | COMEDY | 1604 |
| Much Ado about Nothing | COMEDY | 1598 |
| Othello | TRAGEDY | 1604 |
| Pericles | HISTORY | 1608 |
| Richard II | HISTORY | 1595 |
| Richard III | HISTORY | 1592 |
| Romeo and Juliet | TRAGEDY | 1594 |
| The Merchant of Venice | COMEDY | 1596 |
| The Merry Wives of Windsor | COMEDY | 1600 |
| The Taming of the Shrew | COMEDY | 1593 |
| The Tempest | COMEDY | 1611 |
| The Winter's Tale | COMEDY | 1610 |
| Timon of Athens | TRAGEDY | 1607 |
| Titus Andronicus | TRAGEDY | 1593 |
| Troilus and Cressida | TRAGEDY | 1601 |
| Twelfth Night | COMEDY | 1599 |
| Two Gentlemen of Verona | COMEDY | 1594 |
So that's all done! The key thing is that once you have the CSV data inside your program as a simple
Java data strucuture (a List of String[] arrays), then it becomes very easy to work with.
Here is a list of all the files used in this example. Note that the actual source code is slightly longer than the examples above, which have been abridged for clarity.
plays.htm from the plays.csv fileFeel free to experiment with these files and see what happens. You can also use them as the basis for your own solutions.
Please feel free to email us at examples@ricebridge.com if you have any questions or comments about this example.
Got a question for us?
Just Ask!
$15 Gift Certificate for every bug you find.