WDDX is an XML vocabulary and technology that was developed by Allaire in 1998 for describing a complex data structure such as an array, an associative array (that is, ColdFusion structs), or a recordset in a generic fashion.
WDDX was developed to be an open method for exchanging complex data between a variety of applications and languages, and it is currently supported in ASP, ColdFusion, Java, Python, Perl, Flash, PHP, and Tcl/Tk!
WDDX offers developers several advantages over creating their own XML language for exchanging data: It's already well supported, is very lightweight, and is extremely easy to use. You need to have almost zero knowledge of XML to use it.
Check out www.openwddx.org. This is the WDDX site where you can download the WDDX DTD and SDK. You can also find answers to numerous questions in its forums and email list.
WDDX supports two very basic concepts for information exchange. The first of these is serialization of data from its native format (that is, a query recordset) into a WDDX XML format. The second is deserialization, or the transformation from a WDDX XML document into a native data type such as a CFML structure or a Java associative array.
In ColdFusion, you work with WDDX via the CFWDDX tag, which supports the following functions
A typical CFWDDX tag used to convert a CFML query object to WDDX looks like this:
<cfwwdx action="cfml2wddx" input="#MyQueryObject#" output="WddxTextVariable">
In this example, MyQueryObject is the name of the query object variable, and WddxTextVariable is the name of the variable in which to store the resulting WDDX XML.
Validating WDDX Data
The CFML Reference describes these objects and their methods in detail.
Listing 20.16 wddxtojs.cfm
Transferring Data from the Browser to the Server
In this section, we are going to a create a more complex example that will demonstrate how you can use WDDX to exchange information between a web browser and the application server. In Listing 20.17, we serialize form field data, post it to the server, deserialize it, and then display the data. In reality, you can create very complex and sophisticated applications that exchange a large amount of data in this same way between the browser and the server using this same approach.
Listing 20.17 browsertoserver.cfm
Storing Complex Data
A major use of WDDX in ColdFusion is to store complex data. The simple example in Listing 20.18 uses WDDX to store complex data, a data structure that contains arrays as a string in a client variable. It uses the CFDUMP tag to display the contents of the structure before serialization and after deserializa-tion. It uses the HTMLEditFormat function in a CFOUTPUT tag to display the contents of the client variable. The HTMLEditFormat function is required to prevent the browser from trying to interpret (and throw away) the XML tags in the variable.
Listing 20.18 wddxStruct.cfm
<cfapplication name="relatives" clientmanagement="Yes"> <cfscript> Order = structNew(); Order.OrderID = 002; Order.OrderNum = 002; Order.OrderDate = "01/01/01"; Order.ShipDate = "01/28/01"; Order.ShipName = "Francis"; Order.ShipAddress = "128 Maine Street"; </cfscript> <br> <cfdump var="#Order#"><br> <br> <!--- Convert data structure to string form and save it in the client scope ---> <cfwddx action="cfml2wddx" input="#Order#" output="Client.wddxOrder"> The contents of the Client.Order variable:<br> <cfoutput>#HtmlEditFormat(Client.wddxOrder)#</cfoutput><br> <br> <!--- Now read the data from client scope into a new structure ---> <cfwddx action="wddx2cfml" input="#Client.wddxOrder#" output="OrderBack"> A dump of the wddxOrder structure <br> generated from client.wddxOrder<br> <br> <cfdump var="#OrderBack#">
As you can see, WDDX is probably the most simple and easy-to-use XML technology out there. Using WDDX, you can create simple content and data syndication applications, e-commerce systems, internal data-storage systems, and much more. You can even use WDDX along with web services as a payload.