There are quite a few implementations of XQJ out there. To keep things simple, let's pick a free open source Native XML Database with an XQJ Driver, like the Sedna XML Database.
Goto Sedna's website (http://www.modis.ispras.ru/sedna/) and download the latest and greatest version of the Sedna XML database for your operating system.
Once you have downloaded and installed (or extracted) the Sedna XML Database binaries, it's time to create and run a Database called 'test'.
In a console/shell, navigate to your Sedna installation root directory, i.e. 'sedna' and execute:
Linux:
[user@sedna ~]# cd bin [user@bin ~]# se_gov GOVERNOR has been started in the background mode [user@bin ~]# se_cdb test Creating a data base (it can take a few minutes)... The database 'test' has been created successfully [user@bin ~]# se_sm test Starting database recovery or hot-backup restoration... Database is in consistent state. Starting... SM has been started in the background mode
Windows:
C:\path\to\sedna> cd bin C:\path\to\sedna\bin> se_gov GOVERNOR has been started in the background mode C:\path\to\sedna\bin> se_cdb test Creating a data base (it can take a few minutes)... The database 'test' has been created successfully C:\path\to\sedna\bin> se_sm test Starting database recovery or hot-backup restoration... Database is in consistent state. Starting... SM has been started in the background mode
To perform XQueries, ideally we need some data to query against, so let's get some XML data and load it into our database instance.
Take the following file and save it to the 'src' directory on your hard-disk
books.xml
<?xml version="1.0"?> <catalog> <book isbn="0470192747"> <author>Kay, Michael</author> <title>XSLT 2.0 and XPath 2.0 (4th Edition)</title> <genre>Computer</genre> <price>33.99</price> <publish_date>2008-06-03</publish_date> <description>This book is primarily a practical reference book for professional XSLT developers.</description> </book> <book isbn="0596006349"> <author>Walmsley, Priscilla</author> <title>XQuery</title> <genre>Computer</genre> <price>38.50</price> <publish_date>2007-03-30</publish_date> <description>This in-depth tutorial not only walks you through the XQuery specification, but also teaches you how to program with this widely anticipated query language.</description> </book> <book isbn="059652112X"> <author>Kalin, Martin</author> <title>Java Web Services: Up and Running</title> <genre>Computer</genre> <price>26.99</price> <publish_date>2009-02-23</publish_date> <description>With this example-driven book, you get a quick, practical, and thorough introduction to Java's API for XML Web Services (JAX-WS) and the Java API for RESTful Web Services (JAX-RS).</description> </book> <book isbn="0321356683"> <author>Bloch, Joshua</author> <title>Effective Java (2nd Edition)</title> <genre>Computer</genre> <price>35.99</price> <publish_date>2008-05-22</publish_date> <description>Presents the most practical, authoritative guidelines available for writing efficient,well-designed programs.</description> </book> <book isbn="0141014865"> <author>de Botton, Alain</author> <title>Status Anxiety</title> <genre>Philosophy</genre> <price>9.99</price> <publish_date>2005-01-13</publish_date> <description>The author presents a universal condition of which many of us suffer from called Status Anxiety, investigates it's origins and possible solutions.</description> </book> <book isbn="0201771861"> <author>Rusty Harold, Elliotte</author> <title>Processing XML with Java (SAX, DOM, JDOM, JAXP & TrAX)</title> <genre>Computer</genre> <price>37.99</price> <publish_date>2002-11-14</publish_date> <description>Handing and processing XML in the Java programming language.</description> </book> <book isbn="1887521143"> <author>Poomsan Becker, Benjawan</author> <title>Thai-English and English-Thai Dictionary</title> <genre>Dictionary</genre> <price>14.95</price> <publish_date>2005-04-30</publish_date> <description>With Transliteration for Non-Thai Speakers - Complete with Thai Alphabet Guide</description> </book> <book isbn="0415071771"> <author>Jung, Carl Gustav</author> <title>Psychological Types</title> <genre>Psychology</genre> <price>19.99</price> <publish_date>1992-01-02</publish_date> <description>Essential reading for anyone requiring a proper understanding of Jung's psychology, this was the work in which Jung set out his theory of psychological types as a means of understanding ourselves and the world around us.</description> </book> <book isbn="0596003552"> <author>Pawson, Dave</author> <title>XSL-FO: Making XML Look Good in Print</title> <genre>Computer</genre> <price>26.99</price> <publish_date>2002-08-19</publish_date> <description>Outlines XSL FO's strengths and weaknesses, provides a tutorial and reference guide.</description> </book> <book isbn="0321392795"> <author>Gray, Simon</author> <title>Data Structures in Java</title> <genre>Computer</genre> <price>53.99</price> <publish_date>2006-11-13</publish_date> <description>From Abstract Data Types to the Java Collections Framework.</description> </book> </catalog>
In a console/shell, navigate to your Sedna installation root directory, i.e. 'sedna' and execute:
Linux: (Replace '/path/to/src' with the path to your 'src' directory)
[user@sedna ~]# cd bin [user@bin ~]# se_term -query "LOAD '/path/to/src/books.xml' 'books.xml'" test Bulk load succeeded
Windows: (Replace 'c:\path\to\src' with the path to your 'src' directory)
C:\path\to\sedna> cd bin C:\path\to\sedna\bin> se_term -query ^ "LOAD 'c:\path\to\src\books.xml' 'books.xml'" test Bulk load succeeded
That's all the database setup out of the way.
We will need to create a directory on your hard-disk which will contain example Java sources from this tutorial and the XQJ jar library which they will depend on.
Create a directory on your hard-disk called 'src'.
The binary jar file containing the javax.xml.xquery interfaces and the Sedna XQJ implementation can be found here. Download this and make sure that all source from this tutorial can find it on the classpath when compiling and executing.
Save the following file to the 'src' directory.
ConnectAndDisconnect.java
import javax.xml.xquery.*; import net.cfoster.sedna.xqj.SednaXQDataSource; public class ConnectAndDisconnect { public static void main(String[] args) throws XQException { XQDataSource xqs = new SednaXQDataSource(); xqs.setProperty("serverName", "localhost"); xqs.setProperty("databaseName", "test"); // Default Username and Password for Sedna XQConnection conn = xqs.getConnection("SYSTEM", "MANAGER"); System.out.println("Connected to Sedna."); // Database XQueries and Updates performed here (covered later!) conn.close(); // Closing connection to the Database. System.out.println("Disconnected from Sedna."); } }
Compile and execute this code, making sure that sxqj-beta1.jar is on the classpath, you should get the following output:
Connected to Sedna. Disconnected from Sedna.
That was definitely simple!, let's just recover what happaned there:
NOTE: Like in the JDBC API, it is important close Database Connections, by calling the close() method after you have finished what you are doing.
Right, we've set up the Database instance 'test', loaded some test data 'books.xml' and we have confirmed that we can connect to the database via the XQuery API for Java™.
In the next section we will perform some XQueries against this XML Data.