Using XQuery From Java
The eXist sandbox is an excellent learning tool and can be useful for composing and executing ad hoc queries; however, as you surely discovered when you used SQL, the real power of XQuery comes when it is used in an application. QueryExample.java (available online; see www.ddj.com/code/) is a Java program from the eXist Developer's Guide that can be used to execute XQueries. There are many implementations of XQuery. Because we had already installed eXist to use the sandbox, we chose the XML:DB API that eXist uses. This API can work with any native or XML-enabled database for which a suitable driver has been written. If you examine the Java code, you see that it is roughly analogous to using JDBC. You start by using the following code to load the driver:
String driver = "org.exist.xmldb.DatabaseImpl"; Class cl = Class.forName(driver); Database database = (Database)cl.newInstance(); DatabaseManager.registerDatabase (database);
The only difference between this code and its JDBC counterpart is the last line, which registers the Database object with the DatabaseManager. Each vendor delivers its own implementation of Database, which is the encapsulation of the database driver functionality that is necessary to access an XML database. The DatabaseManager class provides access to a Collection. The Collection interface represents a collection of Resources stored in an XML database. The Resource interface represents a container for data stored within the database.
Next, you get a Collection object using this code:
Collection col = DatabaseManager.getCollection( "xmldb:exist://localhost:8080 /exist/xmlrpc/db"
The URI starts with xmldb: and the rest is vendor specific, but the overall format resembles the URIs you have used in JDBC.
From the collection, you obtain a Service instance:
XPathQueryService service = (XPathQueryService) col.getService ("XPathQueryService", "1.0");
XPathQueryService is an implementation of the Service interface that enables the execution of XPath queries within the context of a Collection or against a single XML Resource stored in the Collection. The Service interface is equivalent to the Statement interface in JDBC.
You then execute the query using the following statement:
ResourceSet result = service.query(args[0]);
The argument to the query() method is a string whose contents is the XQuery. In our example, rather than hardcoding an XQuery, we generalized the program by accepting the XQuery as a command-line argument. The query() method returns a ResourceSet, which is similar to the ResultSet in JDBC. We complete our program by iterating over the ResourceSet and displaying the content of each Resource it contains. The code looks like this:
ResourceIterator i = result.getIterator(); while(i.hasMoreResources()) { Resource r = i.nextResource(); System.out.println ((String)r.getContent()); }
QueryResults (available online) shows the results obtained when you run the program using this command-line argument:
doc("musicians.xml")/musicians/musician[birthDate>="1960-01-01" and birthDate<"1965-01-01"]