Loading

try {
  http:get("/blog/60028183183/xqj-from-zorba")
} catch http:not-found {
  <p>
    404 NOT FOUND.
    Take me home 
  </p>
}
~
~
-- INSERT -- All 6, 17

XQJ from Zorba

Posted 1 year ago

If you’ve ever wondered whether Zorba will support XQJ, now is the time. Starting with version 2.5 Zorba includes a XQJ layer where you can have full access to all the features specified by XQJ. The XQJ API enables Java programmers to execute XQuery against any XML data source. The advantages for Java users include:

  • Executing XQuery against an XML data source and processing the results in Java.
  • Binding XQuery variables from Java.
  • Creating prepared and ad-hoc XQuery expressions.
  • Altering XQuery static and dynamic context behaviour.

Now with this layer enabled for Zorba, you will have access to a fast multi-platform and open-source XQuery engine.

Along with Zorba distribution we are including:

  • (lib)zorba_api[.so/.dll] - Zorba-Java layer library
  • zorba_api.jar - Zorba-Java layer classes
  • zorba-xqj.jar - Zorba-XQJ layer classes
  • xqjapi.jar - XQJ Interfaces Standard

Where can you find these files on your system? On Windows, they are usually installed in:

C:\Program Files (x86)\Zorba XQuery Processor 2.X.0\share\java
C:\Program Files (x86)\Zorba XQuery Processor 2.X.0\share\xqj

On Linux or Mac, you should be able to find them in:

/usr/share/java 
/usr/share/xqj

Your project will need to you set-up one of the following environment variables use the variable according to your system:

  • Windows: SET PATH=%PATH%;[path_to_zorba_api_dll]
  • Mac: export DYLD_LIBRARY_PATH=[path_to_zorba_api_so]
  • Linux: export LD_LIBRARY_PATH=[path_to_zorba_api_so]

Now show your friends how easy is to manage XML. Here is an example of how the Zorba XQJ layer is being used in a single open-close connection:

import javax.xml.xquery.*;
import org.zorbaxquery.api.xqj.*;

// Getting Zorba Data Source
ZorbaXQDataSource xqds = new ZorbaXQDataSource();
XQConnection xqc = xqds.getConnection();

Properties props = new Properties();

XQPreparedExpression exp = xqc.prepareExpression("’Hello from Zorba’");
XQResultSequence res = exp.executeQuery();
while (res.next()) {
System.out.println(res.getItemAsString(props));
}

XQExpression xqe = xqc.createExpression();
XQResultSequence xqs = xqe.executeQuery("1,2,3");
while (xqs.next()) {
System.out.println(xqs.getItemAsString(props));
}

// IMPORTANT - Close to release resources
xqc.close();

This means that you can use Zorba with any other project that uses XQJ. And there is one more thing: we added aXQDocumentManager and a XQCollectionManager class directly from our Zorba framework. We have designed these classes based on the XQJ standard so you can use them directly with XQJ. To see how can you use these classes in Zorba, please refer directly to ourdocumentation. Here is an example that shows Zorba collections in action:

import javax.xml.xquery.*;
import org.zorbaxquery.api.xqj.*;


// Getting Zorba Data Source
ZorbaXQDataSource xqds = new ZorbaXQDataSource();
org.zorbaxquery.api.xqj.XQConnection xqc = (org.zorbaxquery.api.xqj.XQConnection) xqds.getConnection();
XQXmlDataManager xmlManager = xqc.getXmlDataManager();
XQCollectionManager colManager = xmlManager.getCollectionManager();
URI uri;
QName qname;
XQItemType type = null;
try {
uri = new URI("http://www.mod2.com/");
qname = new QName("http://www.mod2.com/", "col2");
type = xqc.createAtomicType( XQItemType.XQBASETYPE_QNAME, qname, uri);
} catch (URISyntaxException e) {
throw new XQException("Error creating QName: " + e.getLocalizedMessage());
}
XQItem colName = xqc.createItemFromString("col2", type);
colManager.createCollection(colName);
XQCollection collection = colManager.getCollection(colName);
colName.close();
XQSequence data = xmlManager.parseXML("Book 1Book 2");
collection.insertNodesLast(data);
xqc.close();

Our XQJ layer passes 100% of the XQJ TCK tests. Give it a try!