Example 6-35: Producing nested XML from a SQL query with XML SQL Utility

import java.sql.*;
import oracle.xml.sql.query.*; 

public class CourseAssignments {
  public static void main (String arg[]) throws Exception {
    // Get the database connection to our XMLBOOK user
    Connection cn = Examples.getConnection();
    String query = "SELECT course, CURSOR(SELECT name,age "+
                   "                        FROM course_assignments b"+
                   "                       WHERE b.course = a.course"+
                   "                     ) AS students"+
                   "  FROM course_assignments a"+
                   " GROUP BY course"+
                   " ORDER BY course";
    // Create an instance of the OracleXMLQuery object
    OracleXMLQuery q = new OracleXMLQuery(cn,query);
    // Set some of its XML Generation options
    q.useLowerCaseTagNames();
    q.setRowsetTag("courses");
    // Print it out and close the connection
    System.out.println(q.getXMLString());
    cn.close();
  }
}