Introducing WDDX


The WDDX format was created in 1998 by Macromedia's legendary Sim Simeonov as a simplified way to use XML for exchanging data among Internet applications. At that time, many of the XML tools commonly available today were just beginning to emerge, and high-level XML technologies such as SOAP, SAX, and XSLT for representing and massaging data were not yet in the mainstream.

The idea was to come up with a simple way of thinking about XML that allowed ColdFusion users to start reaping its benefits right away, without having to learn about XML parsers, DTDs, document object models, entities, namespaces, and so on. Of course, all of those concepts are important and very useful, but Sim realized that plenty of uses for XML could be facilitated without forcing people to get that deep into the theory and vocabulary of it all. He set out to create a system that would sit on top of XML, hiding all the complexities of parsing, creating, and populating XML documents.

The result was WDDX and the <cfwddx> tag, which first appeared in ColdFusion 4.0. In true ColdFusion style, <cfwddx> gave developers the ability to convert data to and from XML in a single step. All productivity, zero theory.

In a nutshell, WDDX's mission is to take any kind of dataa single number or a complex structure of arrays within other arraysand instantly turn it into a chunk of simple XML. That chunk of XML can then be passed from place to place with reckless abandon. When it's time to actually use the data again, it can be read from the WDDX format back to the way it was before the whole process began.

Perhaps the coolest thing about exchanging data with WDDX is that it takes care of preserving data types for you. So, if part of the data started as a date variable on the way into the WDDX format, it ends up as a date variable when it comes back out. This holds true even if the data gets passed between two different programming environments.

For instance, consider a ColdFusion array that holds a number, a date, and an ordinary string. This array can be converted to WDDX and provided to a JavaScript routine on a Web browser. The JavaScript routine can refer to the array just like any other JavaScript-style array. In addition, the date stored in the array is a true JavaScript Date object, and the number is a true JavaScript Number object. The array is successfully "passed" from CFML to an entirely different type of language (JavaScript). The same goes for other types of applications, such as Perl, Active Server Pages, and Java.

Some WDDX Terminology

I'll start off by introducing some WDDX concepts and terminology.

TIP

When talking out loud about WDDX, people often say "Widdux" rather than pronouncing each of the letters.


WDDX Packet

A WDDX packet is any chunk of data stored in the WDDX format. As you will soon learn, a WDDX packet looks similar to any other XML document, as well as to HTML or CFML, for that matter. Because it's tag based, a WDDX packet is simple, is easy to read, and practically describes itself. Each piece of information is surrounded by special opening and closing tags that allow complex data types such as structures and arrays to be stored in the packet. That's pretty hard to accomplish with other text-based formatscomma-separated or space-delimited text, for example. Depending on the nature of the data, it can even be a bit tricky when using a relational database system.

Serializing

Serializing is the process of converting a piece of data into a WDDX packet. To serialize data, you need to use a language or an environment that has access to a function or procedure that knows about the WDDX format and how to serialize data properly. For instance, in a ColdFusion template, the serialization process is performed by the <cfwddx> tag. In other languages and environments, the functions or methods you use to serialize a particular chunk of data are different, but the resulting WDDX packet should be the same and can be understood by any program that supports WDDX.

Consider this example: Serializing the string Hello, World! creates a WDDX packet that includes the string itself, surrounded by a pair of <string> tags, like this:

 <string>Hello, World!</string> 

The strategy of surrounding each value with tags that explain its type enables the WDDX packet to hold descriptions of your data right along with the data itself. That's one of the coolest things about the WDDX format: The packet can describe itself to whatever application needs to read it.

Deserializing

Deserializing is the opposite of serializing. It's the process of pulling the actual data out of a WDDX packet. For instance, if an application needs to "unpack" the Hello, World snippet shown just above, it first looks at the tags to learn what type of data is in the packet. Because WDDX says that this packet contains string data, the application knows it should store the value sitting between the tags as a string. For strongly typed development environments such as Java, C++, and Delphi, the data type is often very important.

Tools and Languages Supported by WDDX

WDDX support is available for a number of languages and development tools. Of course, it's supported by CFML, which means that Macromedia has built WDDX into ColdFusion as the easy-to-use <cfwddx> tag. You'll find the <cfwddx> tag in many of the code listings in this chapter.

There is also a COM object available that brings WDDX functionality to any COM-enabled development tool or application. This means you can use WDDX to share information among ColdFusion, Active Server Pages (ASP), Visual Basic, and applications built with COM-enabled development tools such as Visual Basic, Delphi, Visual C++, and so on. This COM object is capable of performing all the WDDX-related tasks that ColdFusion templates can do natively.

There is a complete Java implementation of WDDX, a Perl 5 package, and support for WDDX is built into PHP 4 natively. Macromedia also provides WDDX support for JavaScript, which enables you to easily make complex, server-side variables visible to your Web pages. In short, you can use WDDX with all of the following:

  • ColdFusion

  • JavaScript

  • Java (including JavaServer Pages, servlets, and JavaBeans)

  • .NET applications

  • Active Server Pages

  • Perl

  • PHP

  • Visual Basic

  • Any other application that can use COM (ActiveX) controls, including Microsoft Office, Visual C++, and Delphi

NOTE

WDDX can also be used to communicate between ColdFusion and other technologies (like Java and COM). To facilitate WDDX communication you will need to add WDDX support to those applications. This is accomplished using the WDDX SDK available from http://www.openwddx.org/.


WDDX or XML?

It's important to understand that WDDX is simply a type of XML document, designed to provide some of the benefits of XML without your having to first define XML schemas and data definitions. As such, WDDX is not so much an alternative to XML as it is a simple way to leverage XML.

When an XML type has been defined and agreed upon, applications should definitely use that type instead of the more generic WDDX. But if no XML type has been defined, WDDX can be an extremely and immediately useful tool.

NOTE

WDDX is used extensively by ColdFusion itself. In fact, ColdFusion's XML configuration files (stored in the lib directory under the ColdFusion root) are all in WDDX format.




Advanced Macromedia ColdFusion MX 7 Application Development
Advanced Macromedia ColdFusion MX 7 Application Development
ISBN: 0321292693
EAN: 2147483647
Year: 2006
Pages: 240
Authors: Ben Forta, et al

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net