code: public static Document getResultSetAsXML(ResultSet rs, String tableName, String keyName) throws Exception {
          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          DocumentBuilder builder = factory.newDocumentBuilder();
          Document doc = builder.newDocument();
          Element results = doc.createElement("Results");
          doc.appendChild(results);
  
          ResultSetMetaData rsmd = rs.getMetaData();
          // add the name of the table
          Element tableNameElement = doc.createElement("table");
          tableNameElement.setAttribute("name", tableName);
          tableNameElement.setAttribute("key", keyName);
          results.appendChild(tableNameElement);
  
          int colCount = rsmd.getColumnCount();
  
          while (rs.next()) {
              Element row = doc.createElement("Row");
              tableNameElement.appendChild(row);
              for (int i = 1; i <= colCount; i++) {
                  String columnName = rsmd.getColumnName(i);
                  Object value = rs.getObject(i);
                  // If the value is null, don't put it in the XML Document
                  if (value != null) {
                      String colType = rsmd.getColumnTypeName(i);
                      Element node = doc.createElement(columnName);
                      if (colType.equals("bytea")) {
                          String byteaStr = "";
                          byte[] imgBytes = rs.getBytes(i);
                          for (int byteIndex = 0; byteIndex < imgBytes.length; byteIndex++) {
                              if (byteIndex != 0) {
                                  byteaStr = byteaStr + ";" + Byte.toString(imgBytes[byteIndex]);
                              } else {
                                  byteaStr = Byte.toString(imgBytes[byteIndex]);
                              }
                          }
                          node.appendChild(doc.createTextNode(byteaStr));
                      } else {
                          node.appendChild(doc.createTextNode(value.toString()));
                      }
                      row.appendChild(node);
                  }
              }
          }
          return doc;
      }
docs: Return an XML document containing the specified ResultSet.
  
  @param rs The specified ResultSet
  @return the XML document
  @throws java.lang.Exception