// Execute SQL Query
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
// Create a ResultSetBuilder
ResultSetBuilder builder = new ResultSetBuilder(rs);
// Configure some parameters...
if (root != null) {
builder.setRootName(root);
}
if (row != null) {
builder.setRowName(row);
}
if (ns != null) {
String namespace = null;
String url = null;
int sep = ns.indexOf("/");
if (sep > 0) {
namespace = ns.substring(0, sep);
url = ns.substring(sep+1);
builder.setNamespace(Namespace.getNamespace(namespace, url));
}
}
if (maxRows > 0) {
builder.setMaxRows(maxRows);
}
for (int i=0; i < attributes.size(); i++) {
String colName = (String) attributes.get(i);
String attrName = null;
if (colName.indexOf("/") >= 0) {
String col = colName;
int sep = col.indexOf("/");
colName = col.substring(0, sep);
attrName = col.substring(sep+1);
}
try { // If it looks like an integer, is the column number
int colNum = Integer.parseInt(colName);
if (attrName == null) {
builder.setAsAttribute(colNum); // attrName = column Name
}
else {
builder.setAsAttribute(colNum, attrName);
}
}
catch (NumberFormatException e) {
// Otherwise it's the column name
if (attrName == null) {
builder.setAsAttribute(colName); // attrName = column Name
}
else {
builder.setAsAttribute(colName, attrName);
}
}
}
// Rename element
for (int i=0; i < elements.size(); i++) {
String colName = (String) elements.get(i);
String elemName = null;
if (colName.indexOf("/") >= 0) {
String col = colName;
int sep = col.indexOf("/");
colName = col.substring(0, sep);
elemName = col.substring(sep+1);
}
try { // If it looks like an integer, is the column number
int colNum = Integer.parseInt(colName);
if (elemName != null) { // It must have an element name
builder.setAsElement(colNum, elemName);
}
}
catch (NumberFormatException e) {
// Otherwise it's the column name
if (elemName != null) { // It must have an element name
builder.setAsElement(colName, elemName);
}
}
}
// Build a JDOM tree
Document doc = builder.build();
// Convert the result to XML (as String)
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
ByteArrayOutputStream output = new ByteArrayOutputStream();
outputter.output(doc, output);