The DOM Model Page 33
March 15, 2002
Under DOM, all XML documents have a root node. Each document
must have one and only one root node. In our sample XML
document, travel.xml, the root node is Recordset, and can be
written in the XML document as <Recordset> ...
</Recordset>. The nesting of the elements within an XML
document is what defines the tree structure. The root node has
one or more child nodes. Each child node can have one or more
additional child nodes. Travelpackage is the child node of
Recordset. Child nodes are nested inside their parent node.
The document tree for the XML document travel.xml is shown
in this figure:
Using PHP's DOM Support
DOM is not a part of the standard PHP configuration. In order to
use the DOM functions, you must configure PHP by specifying the
--with-dom argument. You will need the libxml libraries before
configuring PHP. For Linux and UNIX servers, you can download
the library from: http://www.xmlsoft.org/. If you are running
Windows, then there is a Windows port available: http://www.fh-
frankfurt.de/~igor/projects/libxml/index.html.
Getting DOM to work with Windows and Apache is not a trivial
task. If you are having problems getting the DOM code to work,
then here are some things to check:
- Open php.ini. There's two things we're looking for,
extension_dir and extension=php_domxml.dll. The first setting to
look at is extension_dir. This defines where the files that
provide the functionality for extensions to PHP, like DOM, are
stored. About half way down the file you should see a section
called Paths and Directories, and the extension_dir directive.
Here, the value for extension_dir is "./", which means the
current folder:
;;;;;;;;;;;;;;;;;;;;;;;;;
; Paths and Directories ;
;;;;;;;;;;;;;;;;;;;;;;;;;
extension_dir = ./ ; directory in which the
loadable extensions (modules) reside
Remember the setting for extension_dir. It doesn't really matter
where the directory is, but you should make sure that the
php_domxml.dll file is in there.
A little further down is a section titled Windows Extensions.
Look for the entry extension=php_domxml.dll. Make sure there
isn't a semi-colon (;) in front of it. If there is, delete the
semi-colon and then save and exit the php.ini file.
- Make sure libxml.dll and iconv.dll are in
the C:\WINNT\System32\ directory.
PHP's DOM XML functions are still experimental (as of PHP
4.0.6). The actual usage of DOM within PHP may change when
they've matured.
DOM Example Code
The DOM API enables very easy reading and writing of XML. We
will use DOM to create an XML file, parse that same file,
convert it to HTML, and display it as a table.
Writing XML with DOM
The first thing we have to do is to create the new DOM object.
The string in the parenthesis becomes the version number for the
XML declaration in the resulting XML file:
<?php
$doc = new_xmldoc("1.0");
Then we have to define the root node:
$root = $doc->add_root("Recordset");
Now, let's add branches to the tree by using new_child(). This
function has two parameters, the first one becomes the name of
the node and the second one becomes the value. In this example,
we want the child under Recordset to be Travelpackage, and it
doesn't have any contents so we leave the string empty. If there
isn't any data for the tag, we have to include the empty string.
If the string is left empty, the XML file will not be well-
formed:
$one = $root->new_child("Travelpackage", "");
SAX Example Code Page 32
Professional PHP4 Programming
The DOM Model Page 34
|