Tub 'o' Dogs

48. XML databases with eXist and ColdFusion

eXist is the first open source XML database to support XQuery, a query language whose specification is still in draft, but apparently approaching finality.

eXist is the first product to meet Torchbox's requirements for an XML repository in environments where our Postgres/XPath combination is not viable; for example when the client specifies a Windows platform or an Oracle or SQL Server back-end:

As a bonus, eXist provides a very simple and effective REST API. Here's a ColdFusion component which abstracts that API a little further:

xmldb.cfc

And here's an example usage:

<cfset xmldb_uri = "http://localhost:8080/exist/servlet/db/beat_combos/">
<cfset mydoc = '<beatles>
<beatle id = "1">john</beatle><beatle id = "2">paul</beatle>
</beatles>'>

<cfset xmldb = createobject("component", "xmldb")>

<!--- insert an XML document --->
<cfset xmldb.insertXML(xmldb_uri, mydoc, "beatles")>

<!--- return results of XQuery statement --->
<cfset myquery = "//beatle">
<cfdump var = "#xmldb.execute(xmldb_uri, myquery)#">

<!--- move document within database --->
<cfset xmldb.moveXML(xmldb_uri, "beatles", "beatles2")>

<!--- delete moved document from database --->
<cfset xmldb.deleteXML(xmldb_uri, "beatles2")>

The XML2Query() method which is optionally invoked by xmldb.execute() is a reworking of my old XML query tag which returns a ColdFusion recordset from an XPath query. Handling data-orientated XML documents like relational tables still seems a very convenient model to me; I'm surprised there aren't analogs in other languages.