FBQuery2XML - by Jeff Peters (jeff@grokfusebox.com)

FBQuery2XML is a simple wrapper that converts the CFML query returned by a Fusebox query file into an XML dataset. It is intended to facilitate the use of existing queries in Ajax applications. Just place it in the same directory with your Fusebox queries to enable its use.

When using FBQuery2XML, the query's data is contained in an XML dataset with the root node <recordset>. Each record is contained in an element <record>. Each field is contained in an element named with the field's name, with the value of the field contained in the element's text node, in CDATA-encoded format. This ensures that any field contents can be successfully retrieved.

If an error occurs while processing the query, FBQuery2XML returns it as an XML dataset in the format <error>Error Message</error>, where Error Message is the thrown error's message text. This allows errors to be passed through an XmlHTTPRequest call and handled naturally on the JavaScript side of the application.

Syntax

FBQuery2XML is primarily intended to be called from an Ajax application. Essentially, you just call its URL with any parameters required by your query. For example, here's a script that creates a dataset with Adobe Spry:

<script type="text/Javascript">
  var dsGuitars = new Spry.Data.XMLDataSet("store/FBQuery2XML.cfm?queryName=qryGetGuitars&dsn=ggallery", "recordset/record");
</script>

The query fuse's file name (without extension) must be the same as the recordset it returns. So if the fuse's name is qryFoo.cfm and its <cfquery> tag has a name attribute value of qryBar, FBQuery2XML will fail.

FBQuery2XML is also technically a custom tag, in that it expects its inputs to come in the Attributes scope. So it can also be used to convert a database query into XML data within CFML:

<cfsavecontent variable="xmlData"> 
  <cf_FBQuery2XML queryName="qryGetGuitars" dsn="ggallery" dbt="Oracle">
</cfsavecontent>

In this context, the FBQuery2XML.cfm file should be in the same directory as the query file, as opposed to being placed in a common Custom Tags directory.

NOTE: There are some conventions that are observed by FBQuery2XML that you may need to change to conform to your local environment. It expects request.dsn and request.dbt variables to exist. Also, you may need to modify your anti-sidestepping code (usually found in Application.cfm) to allow direct calls to FBQuery2XML to run--again, this is a local convention and will depend on your particular approach to Fusebox.

Happy Fuseboxing,
- Jeff