WDDX ColdFusion Help

Web Distributed Data Exchange, or WDDX is an open standards-based XML technology introduced by Allaire for the easy transfer of complex data structures between disparate systems. It requires relatively little developer time on either end to implement and enables the creation of richly interactive applications that pull data from many ! sources. This chapter will introduce the basic concepts behind WDDX discuss potential applications of this technology, and dissect examples of WDDX in action.

Understanding WDDX

WDDX is a dialect of Extensible Markup Language (XML). If you’ve been involved in software development at all in the past year, chances are you’ve heard of XM~ over and over with little in the way of practical examples.

At its most basic XML is an extremely flexible markup language in which you get to define your own tags. Chemists have already agreed on a set of XML tags, known as Chemical Markup Language (CML), for marking up chemical formulas. Similar developments are underway for industries such as finance and insurance. All of these
industry-specific markup languages provide ways to define data structures common to that industry, such as a financial transaction, an insurance claim and the like.

Where does WDDX fit in? WDDX is not aimed at a particular industry or type of data. Instead, it is a generic way of describing data structures that are common to programming languages. It allows a text-based and platform-neutral representation of not only simple data types such as numbers, strings, and dates, but more complex data types such as arrays, recordsets, and structures (or associative arrays, or collections, or dictionary objects, or whatever they’re called in your language of choice). ‘

If we had to choose one word to describe WDOX it would be pragmatic. WDDX was created by people who needed a working solution now. That’s exactly what they got. . WDOX is practical does what it’s designed to do and works well now. Unless WODX becomes the standard it may eventually be made obsolete by an official standard in XML-based data interchange. Such Ii standard is probably a year of arguing away and even when (and if) a standard is finalized, that will not cause WOOX to cease working. The version stamp built into WODX will allow future versions to be compatible with such a standard. All that existing applications would require would be to update WDOX parsers .

The Purpose of WDDX

Now that you’ve got a fair idea of what WDDX consists of, the next question is: What is WDDX good for? The answer: Plenty

Consider the following sce-nario: Your business partner has data on a client’s Microsoft Active Server Pages-based Web site that you need to retrieve on a regular basis and incorporate into your company’s ColdFusion-based intranet. At the same time, you need to make available to another business partner a subset of your intranet’s data, consisting of some data from your business partner and some from you~The catch? This partner uses PerL

Sound like a nightmare? Not really. WDDX modules already exist for COM/ ASP and Perl. This means that the-first busiriess partner can write an ASP templa te that selects the appropriate data from their local database and then uses the WDDX COM object to convert their ADO recordset to WDDX and output it to the browser. Back in your ColdFusion-based intranet, you can use CFSCHEDULE to run a template that retrieves the partner’s WDDX page with CFHTTP, parse it into a native ColdFusion recordset, and integrate it With your existing data. Making your data available to the second partner is even easier. Simply create a template that selects the data for this partner, and use CFWoDX to convert your Cold Fusion recordset into a WDDX packet that is output to the browser. Your partner needs only to retrieve this page and use the existing Perl module for WDDX to convert it into a recordset useful in that environment. .

In just a few easy steps, you’ve transferred complex data structures between different programming environments, running on different operating systems, in different parts of the c<?untry (or the world). There’s _evena name for this sort of distributed application- Web_ syndication. The idea is that as WDDX catches- on, popular data repositories on the Web (weather, stock prices, the post office.etc.) will make their data available through a WDDX interface, so that programmers can integrate the data into their own custom applications with a minimum of forehead beating. As you’ll see later, Allaire has already done this with the successor to their popular Tag Gallery the Developer’s Exchange. Another important benefit of WDDX in this situation is that none of the developers need to know the fir~t thing about manually coding in XML. They don’t have to invent tags, understand the XML Document Object Model, or validate against a Document Type Definition (DID).

It .rilay have occurred to you thatoneof the first.requirements for jumping on the WDDX bandwagon is support in your programming environment for WDDX. Fortunately support is widespread and growing almost daily. To take a page from the official WDDX FAQ available..

“Currently the WDDX SDK provides modules for JavaScript l.x,ColdFusion 4 COM Perl and Java. Note that with the COM module WDDX can be used from within Active

Server Pages Visual Basic Delphi Java Power Builder and C++ ..Allaire does not Currently indicate they have plans to implement modules for other languages but using the SDK any developer will be able to build a module for any language platform that din support the core WDDX data types. Projects are reportedly underway to support WDDX with Python and PHP.”

Does all this mean that you need to be working on a complicated distributed application to take advantage of WDDX? Not at all. Consider another scenario; Imagine that you need to write a small application in ColdFusion that requires a structured data store, but you don’t want to tie yourself to a particular database. If you’ve got data in an existing relational database, simply select it with CFQUERY convert it to WI?DX with CFWDDX, and save it to a text file with CFFILE. If there’s no pre-existing data, you can create your recordset on the fly before saving it as WDDX. More on this later ..

A more realistic example might be this: You’re working with a client w~~ will not allow you to modify their pre-existing database, yet they want you to store additional data in the one they have. If there’s a long text field, you can simply create a structure that contains the new data, and store that as a WDDX packet in the pre-existing text
field. You ould take this further in a new database and store all the data that does not . need to be searched in a single field as .WDDX, so that only one field need be retrieved when displaying the record details

Using WDDX with ColdFusion

Let’s examine some of these great-sounding ideas in terms of actual code. If you’ve been doing ColdFusion development for long, chances are you’ve visited Allaire’s Developer’s Exchange (http://www.allaire.com/developer/gallery.cfm •. At the Developer’s Exchange, Cold Fusion developers can find custom tags and other
CF-related utilities written by enthusiasts like themselves. True to its philosophy, Allaire has already created a WDDX interface to the data behind the Developer’s Exchange. No official documentation exists on how to use this interface, but with a bit of experimentation it’s possible to provide your own interface to the Developer’s Exchange within the context of your own application.

To that end, let’s examine a template that provides a simple interface to the live data .coming from Allaire’s Web site The first step is to look at-the raw data you’re getting back so that you know what you’re dealing with. The ‘WODXinterface can be reached

This template takes two URL parameters (that we know of). The first parameter 10 specifies the Object ID of the object you want to retrieve. In Developer’s Exchange parlance an object can be either one item (for example, a custom tag) or a category that contains a list of items. The second URL parameter is useful only for the item list and is called tags. The tags variable is passed directly to the MAXROWS parameter of a CFQUERY on Allaire’s side. If you do not specify a tags variable, it defaults to a maximum of five records in any list returned.

Here, the real world intrudes on our example. Because you’re downloading data from a remote s~rver, and because of the unreliable nature of the Internet, the possibility exists that your request will time out before you’ve downloaded the entire list of several hundred custom tags. For this reason, you can use the tags URL parameter to specify a maximum of 80 records returned. Because the server might still be down, use the CFTRY and CFCATCHtags to handle these potential errors. Also, there is a problem with the current implementation of this WDDX interface: There does not seemto be a way to specify a starting row to the remote. cfm template. Because you cannot download the entire list
at once and still achieve acceptable response times, you can’t ever see the entire list of custom tags. This is an unpublished interface, likely still undergoing revisions, so there may be a way to specify a starting record (or will be soon), but in any case, this is something to keep in mind when designing your implementations.

Now that you know all the problems you’re dealing with in this block of code, what are you actually doing here? First, you’re using CFHTTPto retrieve remote. cfm, passing the ObjectID you would like on the URl. Second, you’re passing the results of the CFHTTPtag to CFWDDXw,ith the command to convert from WDDX to CFML, and putting the resulting object into the obj Struct variable. The next step is to display the data you have. Remember, there are two types of data: an individual item (encoded as a structure) and a list of items (encoded as a recordset containing WDDX packets). A call to the Is Struct function will tell you which type you’re dealing with. First the list of items:

Putting It AIl Together

<CFIF IsDefinedC’URL.ID’»
<!—
The tags parameter is passed directly to a MAXROWS
parameter in a CFQUERY on the other end. Here it is
limited to 80 records to avoid timeouts. There does
not seem to be a way to specify a starting .
row to Allaire’s remote.cfm template, or we could
page through the records.
—>
.<CFTRV>
<CFHTTP URL-.http://www.allaire.com/developer/gallery/
remote. cfm?ID-#URL. ID#&tags=80′
METHOD-‘GET’

RESOLVEURL-‘fa1se’>
~CFWDDX ACTION:’WDDX2CFML’
INPUT=’#CFHTTP.Fi1eContent#’
OUTPUT=’objWDDX’>
<CFCATCH TYPE:’Any’>
<CFABORT SHOWERROR-‘<B> An error occurred </B><P>Try reducing the number
of tags retrieved in CFHTTP, or adding a RequestTimeOut parameter to the URL.</P>’>
</CFCATCH>
</CFTRY>
</CFIF>
<!DOCTYPE HTML PUBLIC ‘-//W3C//DTD HTML 4.0 Transitiona1//EN’>
<HTML>
<HEAD>
<TITLE>B rowse All aire Deve 1oper Exchange</TITLE>
<STYLE TYPE=’text/css’>
body {
font-family: ‘Aria1’, ‘Helvetica’, ‘sans-serif’;
font-size lOpt;
}
</STYLE> .
<SCRIPT LANGUAGE-‘JavaScript’ TYPE~’text/javascript’>
<!–
function togg1eSourceC) (
var WDDXSource = document.a11.WDDXSource;
WDDXSource.sty1e.visibi1ity – CWDDXSource.sty1e.visibility — ‘hidden’) ?
‘visible’ : ‘hidden’;
}
//–>
</SCRIPT>
</HEAD>
<BODY BGCOLOR=’#FFFFFF’>
<CFIF Not IsDefinedC’URL.ID’»
<P>This template demonstrates putting your own face on data from another site. In
this l.:a5e,
we’re retrieving WDDX data from the <A HREF-.http://www.allaire.com/developer/
ga11ery’>Al1aire Developer’s Exchange</A>, and displaying it within the
context of our own pages. If it seems slow, it’s because for each request, the CF
Server must
retrieve data from the Allaire Web site. For a real application, you might want to
keep a copy
of the remote data in a local database for better response times tq your users.</P>
<B>Se1ect Category</B>
<UL>
<LI> <A HREF-‘exchange_browse.cfm?ID-6809’>Applications</A>
<LI> <A HREF-‘exchange_browse.cfm?ID-6800’>Custom Tags</A>
<LI> <A HREF-‘exchange_browse.cfm?ID-6781′>Visual Tools</A>
<LI> <A HREFa’exchange_browse.cfm?ID-6792’>Web Content</A>
</UL>
<CFELSE>
<CFIF Not IsStruct(objWOOX»
<! — Loop through recordset and di splay an object names —>
<B>Select an item:</B>
<UL>
<CF04TPUT QUERY-‘objWODX’>
<CFWODX ACTION-‘WOOX2CFML’
INPUT-‘#WDOXOBJECT#’
OUTPUT-‘objStruct’>
<LI> <A
HREF-‘exchange_browse.cfm?IO-#objStruct[‘OBJECTIO’]#’>#objStruct[‘NAME’]#</A>
</CFOUTPUT>
</UL>
<CFELSE> .
<!— Display item detai1 —>
<CFOUTPUT><H3>#objWODX[‘NAME’]#</H3></CFOUTPUT>
<CFLOOp·COLLECTION-‘#objWODX#’ ITEM-‘value’>
<CFOUTPUT><B>#value#:</B> #objWOOX[value]#<BR></CFOUTPUT>
</CFLOQP>
</CFIF>
<P><SPAN ID-‘sourceClick’ STYLEa’font-weight: bold; cursor: hand’
.onClick-‘toggleSource()’>Click here to view WODX source.</SPAN></P>
<DIY ID-‘WODXSource’ STYLE-‘visibility: hidden’>
<CFOUTPUT>#HTMLEditFormat(Trim(CFHTTP.FileContent))#</CFOUTPUT>
– </DIY>
</CFIF>
</BOOY>
</HTML>

Posted on November 16, 2015 in WDDX

Share the Story

Back to Top
Share This