XQJ Tutorial Part I: An XQJ Introduction



The XQuery API for Java (XQJ), currently in development under the Java Community Process as JSR 225, lets programmers use XQuery for XML processing and data integration applications, with full support for the Java SE and Java EE platforms. XQJ allows a Java program to connect to XML data sources, prepare and issue XQueries, and process the results as XML. This functionality is similar to that of JDBC Java API for SQL, but the query language for XQJ is XQuery.

Let's start with a simple XQJ application — the XQuery/XQJ version of "Hello World!":

XQDataSource xqjd = new DDXQDataSource();
XQConnection xqjc = xqjd.getConnection();
XQExpression xqje = xqjc.createExpression();
XQSequence xqjs = xqje.executeQuery("'Hello World!'");
xqjs.writeSequence(System.out, null);
xqjc.close();

You might recognize some of the JDBC concepts in the code example above. Indeed, the XQJ expert group decided to make XQJ stylistically consistent with JDBC. As a result, XQJ has JDBC-like concepts such as:

  • Datasources
  • Connections
  • Expressions and prepared expressions

But at the same time, XQuery is not SQL, and as such XQJ differs from JDBC in some areas, including:

  • Data model
  • Typing
  • Static context
  • Error handling

We'll touch on each of these concepts throughout the remaining chapters in this tutorial. So, let's get started!