XQuery API for Java (XQJ)

XQJ is a standard for calling XQuery from Java. XQJ is to XML data sources what JDBC is to relational data sources. It provides a standard set of classes for connecting to a data source, executing a query, and traversing through the result set. It is being developed using the Java Community Process as JSR 225 and is currently in the Early Draft Review 2 stage.

Example 22-4 shows an example of Java code that connects to an XML data source and iterates through the results.

Example 22-4. XQJ example

// connect to the data source
XQConnection conn = xqds.getConnection( );

// create a new expression object
XQExpression expr = conn.createExpression( );

// execute the query
XQResultSequence result = expr.executeQuery(
    "for $prod in doc('catalog.xml')//product" +
    "order by $prod/number" +
    "return $prod/name");

// iterate through the result sequence
while (result.next( )) {

    // retrieve the atomic value of the current item
    String prodName = result.getAtomicValue( );
    System.out.println("Product name: " + prodName);
}

For more information on XQJ, see the specification at http://jcp.org/en/jsr/detail?id=225.

Get XQuery now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.