Building XML Trees With PHP (
Page 1 of 11 )
Need to manipulate XML document trees, but don't have the DOM extension compiled
into your PHP build? Take a look at XMLTree, a PEAR class that allows you to create
and manipulate XML document trees without requiring the PHP DOM extension. If you've been following past issues of this column, you're probably already
aware of the numerous things you can do with XML and PHP. You also probably already
know the basics - that there are two ways XML can be parsed, either via the Simple
API for XML (SAX) or the Document Object Model (DOM); that each method has pros
and cons, and that the selection of a parsing technique depends largely on the
application; and that PHP today supports both parsing methods, making it a versatile
and flexible tool for XML application development.
Now, by default, all newer versions of PHP come with the XML SAX parser enabled;
however, the DOM module needs to be explicitly turned on at compile time. If you're
working in a development environment that is entirely under your control, this
is not a problem - all you need to do is log in as root, recompile PHP and get
cracking.
However, if you're working in a shared environment - for example, on your Web
hosting provider's server - then it's quite likely that you won't have super-user
access to the system. And since Web hosting companies are (understandably!) picky
about compiling new software on their production servers, if your default PHP
environment doesn't already have DOM support compiled in, it's unlikely that you're
going to get it any time soon.
So, a creative solution is needed. Which is where this article comes in.
Over the next few pages, I'm going to be introducing you to a free PHP class
named XMLTree, which allows you to manipulate XML document trees in a manner similar
(though *not* completely identical) to that available in the standard PHP DOM
extension...without requiring you to first recompile your PHP build. As you might
imagine, this can come in handy in certain situations - for example, if you need
a quick and dirty way to build an XML document tree from an external data source,
like a MySQL database or a structured text file. So keep reading - you might find
the rest of the show interesting!