This class is a utility to turn a Java ResultSet into xml. It's returned as a String in the following format:


where the column names are taken from the ResultSet. THIS JAR FILE should be placed in your classpath and then you can call

String s = net.proteanit.sql.XMLUtils.resultSetToXml(rs);

where 'rs' is your ResultSet.

You can also do

String html = net.proteanit.sql.XMLUtils.resultSetToHtml(rs);

and get an html table

The jar is a 'fat jar', as it uses JDOM. It's also executable, so if you do

java -jar rs2xml.jar

you will get a demo rowset of random data.

import org.jdom.Document;
import org.jdom.Element;

import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

public class XMLUtils {
    private static String ROOT_NODE_NAME = "dataset";
    private static String ROW_NODE_NAME = "row";

    public static String resultSetToXML(ResultSet rs) {
        String result = null;

        try {
            Document root = new Document(new Element(ROOT_NODE_NAME));
            ResultSetMetaData metaData = rs.getMetaData();

            // Get all rows.
            while ( {
                Element row = new Element(ROW_NODE_NAME);
                // Add to root

                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    // Use the column label as the node name
                    Element val = new Element(metaData.getColumnLabel(i));
                    // Use the String value of the data as the value node's child
                    val.setText("" + rs.getObject(i));

            XMLOutputter out = new XMLOutputter();
            result = out.outputString(root);
        } catch (Exception e) {

        return result;

    // Only for demo/testing
    public static void main(String[] args) throws Exception {
        ResultSet d = new net.proteanit.sql.DummyNRowCol(4, 10);