January 19, 2021
Hot Topics:

Converting JDBC Result Sets to XML, Page 2

  • By Jeff Ryan
  • Send Email »
  • More Articles »

Sample Client

To test our result-set-to-XML method, we need a sample client program. I've created a simple class called CustomerDAO. DAO is a J2EE pattern that stands for "Data Access Object." It is used to encapsulate a resource such as a customer database. When a CustomerDAO object is constructed, it connects to our Pointbase database. When it is finalized, it closes the connection.Its public API is a getCustomerList() method that is implemented as follows:

public Document getCustomerList()
   Document doc = null;

      Statement stmt = conn.createStatement();
      ResultSet rs = stmt.executeQuery("SELECT * from customer_tbl");

      doc = JDBCUtil.toDocument(rs);

   catch (Exception e)

   return doc;

This method executes a very simple query asking for all rows from the customer table. The result set returned from the query is converted to a Document by the JDBCUtil.toDocument() method.

Here is a sample method for testing the CustomerDAO and JDBCUtil objects:

public static void main(String argv[]) throws Exception
   CustomerDAO dao = new CustomerDAO();

   Document doc    = dao.getCustomerList();


We simply construct a CustomerDAO, and ask it to return a customer list as a DOM. JDBCUtil.serialize() is another method in the utility class that serializes the DOM as a string.

Here is sample output from our test:

<?xml version="1.0" encoding="UTF-8">
      <ADDR_LN1>490 Rivera Drive</ADDR_LN1>
      <ADDR_LN2>Suite 678</ADDR_LN2>
      <NAME>Livingston Enterprises</NAME>
      <ADDR_LN1>9754 Main Street</ADDR_LN1>
      <ADDR_LN2>P.O. Box 567</ADDR_LN2>

Bonus Material!

Click here for the sample JDBCUtil and CustomerDAO classes. You'll also notice some bonus material included in the archive not covered in the article. I was curious about the performance difference in returning an XML DOM versus an XML string. It turns out that returning a string is four to five times faster. The JDBCUtil.toXML(ResultSet) method performs this function. The CustomerDAOTester class uses a StopWatch object to compare String and DOM method invocations.

Should you return a DOM or a String? It really depends on your situation. Often, you will need to merge the results from multiple result sets. You may need to transform the results via XSLT. If so, a DOM would be appropriate. In the situations where a String will do, be careful, because you will need to deal with encoding issues that the DOM API handles for you.


Increasingly, relational databases are providing features for returning the results of queries as XML documents or strings. The features in these products would be more robust and optimized far beyond the simple example shown here. If you have such a database and have purchased this capability, I would encourage you to investigate it. If not, start with the example shown here and grow your own.


XML is a common data format for passing data between components on disparate platforms. Relational databases are the most common way of persisting operational data. How do you bridge the gap between the two standard structures? Your relational database may have such capability. If so, learn how to exploit this feature. If not, I've shown how to use JDBC and DOM API to build your own capability. The rest is up to you!

About the Author

Jeff Ryan is an enterprise architect for Hartford Financial Services. He has twenty years experience designing, developing, and delivering automated solutions to business problems. His current focus is on Java, XML, and Service Oriented Architecture. He may be reached at jeffreyjryan@aol.com.

Other Articles Written by Jeff Ryan

Page 2 of 2

This article was originally published on March 22, 2004

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date