XML and PHP
September 4, 2002
|
Learn about Web Distributed Data eXchange (WDDX) in this
excerpt from XML and PHP. From New Riders.
|
From Publisher
XML and PHP is a clear, concise guide to the synergies between XML
and PHP, many of which are not immediately visible to intermediate
developers. XML and PHP demonstrates how PHP and XML can be combined
to build cutting-edge Web applications. It includes detailed
explanations of PHP's XML extensions, together with illustrations
of using PHP to parse, validate and transform XML markup, traverse
XML data trees, exchange data between Web applications, overlay
remote procedure calls over HTTP, and use free open-source tools
to add new capabilities to your PHP/XML applications.
Chapter 5: PHP and Web
Distributed Data eXchange (WDDX)
"Beam me up, Scotty!"
~Captain James T. Kirk, "Star
Trek"
The preceding chapters have focused primarily on parsing XML documents with a
strong emphasis on producing content for web browsers. Although accomplishing
this is no mean featin fact, it's one of the most popular ways to use
the XML/PHP comboit's just the tip of the XML iceberg.
You'll remember from my opening remarks that XML provides constructs to
encode any type of information in a standard, machine-readable format. This
makes XML the ideal vehicle for information exchange over the web. All
that's needed is an encoding format that is understandable to both sender
and receiver and that can piggyback over standard Internet protocols (HTTP,
SMTP, FTP, and so on).
That's where the Web Distributed Data eXchange (WDDX) comes in. WDDX
provides a standard format for creating XML-based data structures designed for
easy transmission across the Internet. These WDDX data structures are largely
platform-independent, and they can be decoded and used by any application that
understands the WDDX format.
Over the next few pages, I will be examining WDDX in greater detail,
demonstrating how it can be combined with PHP to encode and exchange data across
different systems and platforms.
This chapter marks the transition from merely parsing XML data to actually
using XML as the vehicle for other applications. In addition to detailed
descriptions of how PHP can be used to create WDDX structures, I'll also be
demonstrating some real-life applications of the technology to illustrate its
usefulness and versatility.
WDDX
Invented by Allaire Corp. (makers of the HomeSite HTML editor and the ColdFusion
application development environment), WDDX is " . . . an XML-based technology
that enables the exchange of complex data between web programming languages
. . . 1 It was created in 1998
as an open standard designed specifically to simplify data exchange across different
platforms, and it has quickly gained popularity with web developers for its
elegance and ease of use.
WDDX works by converting language-specific data structures into their
corresponding XML representations. These XML data structures are text-based,
platform-independent entities, and, as such, can be transmitted between
different systems over standard HTTP protocols with minimal difficulty. Any
WDDX-friendly application can read these WDDX packets, and convert them
back into their original form. For example, a Python list could be encoded into
WDDX and transmitted across HTTP to a PHP script, which could decode it into a
PHP array. Or a PHP associative array could be translated into WDDX, sent to a
Perl script, and decoded into a Perl- compliant hash, for use within a Perl
script.
Perhaps an example would make this clearer. Consider Listing 5.1, a
single-line PHP script that defines a variable and assigns it a value.
Listing 5.1 A Simple PHP Variable
<?php
$str = "amoeba";
?>
If this variable were to be converted (or, as the geeks say,
"serialized" or "pickled") into its WDDX representation, it
would look something like Listing 5.2.
Listing 5.2 A WDDX Representation of a PHP Variable
<wddxPacket version='1.0'>
<header/>
<data>
<string>amoeba</string>
</data>
</wddxPacket>
As you can see, this is regular XML markup, with both the variable type and
its value embedded within it. The document element here is the
<wddxPacket> element; it can be separated into distinct header
and data areas. The header contains a human-readable comment, whereas the data
block contains an XML-encoded structure representing the data to be
transmitted.
This WDDX representation can be decoded (or "deserialized" or
"unpickled") and used by any application that understands WDDX. And
so, by creating a standard framework for representing common data structures and
by expressing this framework in XML, WDDX makes it possible to easily exchange
information over the standard Internet backbone.
WDDX consists of two parts: the WDDX specification that defines basic WDDX
structures, and a set of WDDX components for different languages that handles
the translation of language-specific data structures into platform-independent
XML representations. As of this writing, the WDDX specification supports most
commonly used data structures (see the "Not My Type" sidebar for a
list), and WDDX components are available for PHP, Perl, ASP, Java, Python,
JavaScript, and COM. This immediately makes the technology attractive to
developers whose work involves exchanging bits and bytes in multiplatform
environments.
It's important to note that WDDX is not an "official"
specification per se; rather, it's an open standard created and supported
by one company. Despite this, it's fairly popular, primarily for use in
client-server or server-server content publishing systems, or as a wrapper for
data exchange between multiple programming languages. By allowing applications
written in different languages to easily communicate with each other, it also
opens the door to new B2B applications, particularly in the areas of
streamlining business processes and transactions.
For more information on WDDX, you should refer to the official web site at
http://www.openwddx.org/,
which contains the WDDX Document Type Definition (DTD), an SDK (if you need to
implement WDDX on an unsupported platform), and usage examples.
Not My Type
The WDDX specification currently defines the following data types:
Boolean values, represented by the <boolean> element
Numbers, represented by the <number> element
Strings, represented by the <string> element
Date/time values, encoded in ISO8601 format, represented by the <dateTime>
element
Integer-indexed arrays, represented by the <array> element
Structures, or string-indexed arrays, represented by the <struct>
element;
Recordsets, or two-dimensional (rows versus columns) data collections, represented
by the <recordset> element
PHP and WDDX - Page 2
|