Running XQuery from Java applications using the XQJ API and the Oracle XDK implementation

While preparing for the new SOA for Java Professionals training program at AMIS, I was recently working on a section discussing XQuery and more specifically the ability to use XQuery from Java as an alternative to JAXB binding to POJOs on the one hand and DOM based XPath searches on the other.

The XQJ API for XQuery support from Java is relatively new and unknown. Implementations of the API are not yet part of Java 6 SE (I am not sure about Java 7) and the number of resources on the internet is still limited.

This article shows the steps for running a simple Java program for inspecting an RSS feed and printing titles for RSS items that concern Java. It also shows how to configure a Java application in JDeveloper 11g using the Oracle XDK implementation of XQJ.

The RSS feed inspected and dissected in this article could be anyone. It happens to be a feed of recent technical articles on the Oracle Technology Network: http://feeds.delicious.com/v2/rss/OracleTechnologyNetwork/otntecharticle.

The output looks something like:

Image

The Java code for accessing this RSS feed an retrieving item titles for items that discuss a Java related topic is fairly straightforward:

Image

However, in order to successfully compile and run this piece of code, several JAR files need to be gathered and referenced from the project.

The JAR files required for using the XQJ implementation from Oracle XDK are: xqjapi.jar, xqjori.jar, xquery.jar and orai18n-collation.jar. In addition, the Oracle XML Parser V2 library is required.

The first three jars are shipped as part of Oracle 11g RDBMS – and are not separately available as far as I was able to establish. They seem not be shipped with the 11g XE edition, so you need access to a SE or EE installation of the Oracle Database in order to get hold of these XDK jar files. It is not ideal.

The XQuery specific jar-files are in the 11.2.0\dbhome_1\xdk\lib directory:

Image

The orai18n-collation.jar is in the 11.2.0\dbhome_1\jlib directory.

Image

I have copied the jar-files to the lib subdirectory of the JDeveloper project. The configuration of the JDeveloper project references the jar-files:

Image

Now we can compile and run the Java program:

Image

A slightly extended version of the Java program binds the query parameters in the XQuery statement and reuses the prepared expression to also query titles related to the keyword Oracle:

package nl.amis.xquery;


import javax.xml.xquery.XQConnection;
import javax.xml.xquery.XQDataSource;
import javax.xml.xquery.XQPreparedExpression;
import javax.xml.xquery.XQResultSequence;

public class RSSProcessor {
    private static String keyword = "Java";
    private static String rssUrl  = "http://feeds.delicious.com/v2/rss/OracleTechnologyNetwork/otntecharticle";

    public static void main(String[] args) throws Exception {
        XQDataSource xds = new oracle.xquery.xqj.OXQDataSource();
        XQConnection conn = xds.getConnection();
        XQPreparedExpression pEx =
            conn.prepareExpression(" declare variable $doc external;"
                                   + "declare variable $keyword external;"
                                   + "for $c in $doc//item "
                                   + "where fn:contains($c/title,$keyword) "
                                   + "return $c/title");
        java.net.URL doc =
            new java.net.URL(rssUrl);
        java.io.InputStream inpt = doc.openStream();
        ;
        pEx.bindDocument(new javax.xml.namespace.QName("doc"), inpt, null, null);
        pEx.bindObject(new javax.xml.namespace.QName("keyword"), keyword, null);
        XQResultSequence rslt = pEx.executeQuery();
        while (rslt.next()) {
            System.out.println(rslt.getAtomicValue());
        }
        System.out.println("Reuse prepared expression, now to fetch Oracle related titles");
        pEx.bindObject(new javax.xml.namespace.QName("keyword"), "Oracle", null);
        XQResultSequence rslt2 = pEx.executeQuery();
        while (rslt2.next()) {
            System.out.println(rslt2.getAtomicValue());
        }

        conn.close();
        inpt.close();
    }

}

Resources

The original code for processing the RSS feed – as well as instructions for configuring the application – using XQJ comes from this article on Oracle Technology Network: http://www.oracle.com/technetwork/articles/oem/xquery-jdbc-325944.html.

This article on Devx.com introduces the XQJ API: http://www.devx.com/Java/Article/34642/1954. Here – http://www.xquery.com/tutorials/xqj_tutorial/ – you can find an XQJ tutorial.

More details about the XQJ API can be found on http://www.xqjapi.com/ – for example the full Java API Doc: http://www.xqjapi.com/javadoc/. The formal origins are in the JSR 225 specification page: http://jcp.org/en/jsr/detail?id=225.

Download JDeveloper 11gR2 application (including the libraries discussed in this article): XQueryFromJava.

3 Comments

  1. Jose Carlos November 26, 2014
  2. frank September 6, 2013
  3. balopat October 18, 2011