Hello Berkeley DB XML on .NET
If you’re not familiar with Berkeley DB XML (BDB XML), it’s an open source embeddable XML database engine that provides support for XQuery access.
The official BDB XML distribution provides the library in the C++, Java, Perl, Python, PHP, and Tcl languages. Sadly, no .NET version.
I tried to look for a third-party .NET library and found this one from Parthenon Computing. The problem with this product is that it doesn’t seem up-to-date with .NET 2.0 and the most recent versions of BDB XML. Also it’s free for non-commercial use only.
Since I couldn’t find any other .NET libraries I started to think what I could do with the existing ones, and it occurred to me I could cross-compile the Java version using IKVM.NET. Because I use IKVM all the time with Saxon XSLT/XQuery I trust and have a good opinion of this product.
Running the IKVM compiler is very simple. For this task you have to grab db.jar and dbxml.jar (available from the official BDB XML distribution) and execute the following command:
ikvmc -out:dbxml_net.dll db.jar dbxml.jar
And that’s it! You have your .NET API, officially maintained and up-to-date with the latest BDB XML release. The API will have the Java look-and-feel, camelCase for members, no properties, etc. If you can live with that then there’s no problem. Please note that I haven’t tested much this library, but I suspect it shouldn’t have problems, I encourage you to try it and let me know your experience.
Here’s my hello world program, it queries a container I had previously created using the dbxml command-line shell:
using System;
using com.sleepycat.db;
using com.sleepycat.dbxml;
namespace BerkeleyDB_XML1 {
class Program {
static void Main() {
Console.WriteLine("Running {0}...", com.sleepycat.db.Environment.getVersionString());
XmlManager mgr = new XmlManager();
XmlContainer cont = mgr.openContainer(@"c:/temp/bdb-hello/hello.dbxml");
int count = cont.getNumDocuments();
Console.WriteLine("Number of available documents: {0}", count);
XmlQueryContext qctx = mgr.createQueryContext(XmlQueryContext.Eager);
XmlResults results = mgr.query(
@"for $h in collection('dbxml:c:/temp/bdb-hello/hello.dbxml')/hello
return $h", qctx);
while (results.hasNext())
Console.WriteLine(results.next().asString());
cont.close();
mgr.close();
}
}
}
2009-05-26 Update
On ASP.NET you might get an “Unable to load ikvm-native” error message. There are several ways to fix this: a) Add the ikvm-native.dll location to the PATH; b) Copy ikvm-native.dll to %windir%\system32
; c) Add a ikvm:java.library.path
appSetting to Web.config and set its value to the ikvm-native.dll location.