Verifying XML Support
March 1, 2002
Before we get started, make sure that the computer you're
working with was set up to support XML. If you are not sure that
your existing PHP installation has XML support, check
phpinfo.php from Chapter 2. If XML support is set to active, the
output will contain a section similar to the following:
If you do not see the above, then don't worry. We will cover
setting up XML support for each API in the appropriate section.
For further information refer the PHP manual for instructions on
installing PHP with XML support at:
http://www.php.net/manual/en/ref.xml.php
XML APIs Comparison
The three main PHP APIs available for working with XML are SAX,
DOM, and PRAX. SAX and DOM are available in most programming
languages. PRAX is a much newer API and it is a port of the Perl
XML::RAX API.
These XML APIs that are "native" to PHP have some problems:
-
SAX has some problems that can cause a server crash.
-
DOM has memory leaks in the code library libxml. The memory
leaks are in all the PHP releases (current release is 4.0.6), on
most platforms, Windows98/NT/2000, Linux (Redhat, Debian,
Mandrake, SuSE), and xBSD.
-
PRAX does not work very well with complex XML documents.
In spite of these minor difficulties, each of these APIs deals
with XML in different ways, and provides different functions.
Choosing which API to use depends on your application.
Let's now compare some of the more important parts of each API:
|
SAX
|
DOM
|
PRAX
|
| Model
|
Event-driven
|
Document tree
|
Recordset
|
| Installation
|
Installed by default
|
Requires installation
|
Standalone class
|
| Document Size
|
Small to huge
|
Small to medium
|
Small to medium
|
| Code Complexity
|
Moderate to complex
|
Moderate to complex
|
Simple to moderate
|
| Supports Read, Write, Change
|
Only read
|
Yes
|
Only read
|
| Best Fit Application
|
Machine readable and generated XML
|
Complex XML document
|
Machine readable and generated XML
|
| Maturity within PHP
|
Expat has been in PHP since Version
3.0 and it seems to be pretty mature and stable
|
Experimental
|
Very young
|
| XML Format
|
XML 1.0, thus you can use
it for SML too
|
XML 1.0, thus you can
use it for SML too.
|
SML
|
SAX vs. DOM
The main difference between SAX and DOM is how they interact
with an XML file. SAX reads the file in relatively small chunks,
parses them, calls a handler, and performs a task. The task that
is performed by the instantiated parser is up to the developer.
Using SAX we can in theory parse an XML document of infinite
length. In this chapter we will be looking at displaying XML as
a table so we will set up the handlers to do this.
DOM loads the whole XML file into memory. Consequently it places
a higher load on the server. Once the document is in memory we
can interact with it in much the same way we would interact with
a multi-dimensional array. We can add, read, and delete (unlink)
nodes. This difference between APIs won't matter for small XML
files and low volume servers, but when you get large XML
documents you may notice a performance difference.
PRAX vs. SAX and DOM
With PRAX we can quickly parse an XML document with a minimum of
code and complexity. Reading files with PRAX is programmatically
easier than SAX, and doesn't have the memory overhead of DOM.
PRAX is a new API and is relatively unknown. In its current
state I don't recommend using it for commercial or production
use. PRAX is here to provide another example of working with XML
and PHP. We shall go into depth for each one further in this
chapter where we'll discuss each API and look at the PHP code
that will render an XML document into HTML.
SML
Professional PHP4 Programming
The SAX Model
|