NYWC database through JDBC to DOM

import java.sql.*;
import java.io.*;
import org.w3c.dom.*;
import org.apache.xerces.dom.*;
import org.apache.xml.serialize.*; 

public class BiosToDOM {
  public static void main(String[] args) {
    String userName = "elharo";
    String password = args[0];
    String host = "luna.oit.unc.edu";

    try {
      //Requires JDBC MySQL Driver from http://www.worldserver.com/mm.mysql/
      // Connect to the database
      Connection connection = DriverManager.getConnection(
        "jdbc:mysql://" + host + "/NYWC", userName, password);
      Statement statement = connection.createStatement();
      ResultSet bios = statement.executeQuery(
       "SELECT * FROM composers ORDER BY ComposerLastName, ComposerFirstName");
      DOMImplementationImpl impl = (DOMImplementationImpl) 
      DocumentType type = impl.createDocumentType("Composers",
       null, null);
      // type is supposed to be able to be null, 
      // but in practice that didn't work                     
      DocumentImpl composers 
       = (DocumentImpl) impl.createDocument(null, "Composers", type);
      Element root = composers.createElement("Composers");
      // This not only creates the element; it also makes it the
      // root element of the document. 

      while (bios.next()) {
        Element composer = composers.createElement("Composer");
        /* primary key */
        Element name = composers.createElement("name");
        appendTextChild(composers, name, "first", bios.getString("ComposerFirstName"));
        appendTextChild(composers, name, "middle", bios.getString("ComposerMiddleName"));
        appendTextChild(composers, name, "last", bios.getString("ComposerLastName"));
        Element first = composers.createElement("first");
        appendTextChild(composers, composer, "biography", bios.getString("ComposerBio"));
        appendTextChild(composers, composer, "email", bios.getString("ComposerEmail"));
        appendTextChild(composers, composer, "url", bios.getString("ComposerURL"));
        appendTextChild(composers, composer, "title", bios.getString("ComposerTitle"));
        appendTextChild(composers, composer, "state", bios.getString("ComposerState"));
        appendTextChild(composers, composer, "city", bios.getString("ComposerCity"));
        appendTextChild(composers, composer, "country", bios.getString("ComposerCountry"));
        appendTextChild(composers, composer, "zip", bios.getString("ComposerZip"));
        Date expires = bios.getDate("ComposerMembershipExpires");
        String expiresDate = null;
        if (expires != null) expiresDate = expires.toString();
        appendTextChild(composers, composer, "expires", expiresDate);
      try {
        // Now that the document is created we need to *serialize* it
        OutputFormat format = new OutputFormat(composers);
        XMLSerializer serializer = new XMLSerializer(System.out, format);
      catch (IOException e) {
        System.err.println("Serialization failed: " + e); 
    catch (Exception e) {
      System.err.println("Unexpected Exception");

  private static void appendTextChild(DocumentImpl impl, 
   Element parent, String name, String content) {
    // We don't have to worry about illegal characters in the content.
    // The DOM handles that automatically for us.  
    Element e = impl.createElement(name);
    if (content != null) {
      Text textNode = impl.createTextNode(content);


View result in Browser
Previous | Next | Top | Cafe con Leche

Copyright 2000 Elliotte Rusty Harold
Last Modified March 13, 2000