Tutorials

Serializing a Query Result-Set into a WDDX Packet (Page 1 of 1)

There are many uses to the WDDX functionality in ColdFusion. To put it simply, using the CFWDDX tag developers can serialize and deserialize complex data structures into an XML-based format. For the purposes of this article we'll only look at the CFWDDX tag from the standpoint of creating WDDX packets. The uses of the CFWDDX tag range from serializing FORM data and passing that data to an action page(s) to serializing query result-set data and passing the WDDX packet to Flash. View the example code below and then we'll look at what's happening.

<cfheader name="Expires" value="##NOW()##">
<!--- query the database and retrieve the employees --->
<cfquery name="GetEmployees" datasource="EmployeeDB">
      SELECT *
      FROM Employees
      ORDER BY EmployeeID
</cfquery>
<!--- serialize the query result-set into a WDDX packet --->
<cfwddx action="CFML2WDDX" input="##GetEmployees##" output="myWDDXPacket">

<cfif GetEmployees.RecordCount neq 0> <cfoutput>##myWDDXPacket##</cfoutput> </cfif>

Let's breakdown what's happening in the code. The CFHEADER tag, the way that I'm using it above, ensures that the ColdFusion page (hence the database query) is never cached. The next couple of lines show my call to the database selecting all of the employees and then sorting the list by EmployeeID. I then use the CFML2WDDX action of the CFWDDX tag to create an XML representation or WDDX packet of my query result. I simply feed the tag my query called GetEmployees and then tell ColdFusion that I want my output saved in the variable myWDDXPacket. I can do whatever I wish with the packet now. For my particular situation, I had a Flash movie call the ColdFusion page, the ColdFusion page query the database, and the WDDX packet written to the screen. Writing the packet to the screen allowed me to read and parse the packet in Flash so I could display my database information in a clean interface. (There is an in-depth tutorial on how to do this in the Tutorials section)

So there you have it, creating a WDDX packet and populating it with database information is a pretty simple process. But don't stop there! There are many uses for the CFWDDX tag and even other tags and functions that can be used to verify the structure and correctness of your generated packet.

PREVIOUS

RSS