The DOM XML extension has been overhauled in PHP 4.3.0 to better comply with
the DOM standard. The extension still contains
many old functions, but they should no longer be used. In particular, functions
that are not object-oriented should be avoided.
The extension allows you to operate on an XML document with the DOM API.
It also provides a function domxml_xmltree() to turn the
complete XML document into a tree of PHP objects. Currently, this
tree should be considered read-only - you can modify it, but this
would not make any sense since DomDocument_dump_mem()
cannot be
applied to it. Therefore, if you want to read an XML file and write
a modified version, use DomDocument_create_element(),
DomDocument_create_text_node(),
set_attribute(), etc. and finally the
DomDocument_dump_mem() function.
Note:
This extension has been moved to the PECL repository and is no longer bundled with
PHP as of PHP 5.0.0.
Note:
This extension is no longer marked experimental. It will, however, never
be released with PHP 5, and will only be distributed with PHP 4.
If you need DOM XML support with PHP 5 you can use the
DOM extension. This
domxml extension is not compatible with the
DOM extension.
This extension makes use of the
GNOME XML library. Download
and install this library. You will need at least libxml-2.4.14.
To use DOM XSLT features you can use the
libxslt library and EXSLT
enhancements from http://www.exslt.org/.
Download and install these libraries if you plan to use (enhanced) XSLT
features. You will need at least libxslt-1.0.18.
This PECL extension
is not bundled with PHP.
Additional information such as new releases,
downloads, source files, maintainer information, and a CHANGELOG, can be
located here:
http://pecl.php.net/package/domxml.
In PHP 4 this PECL extensions
source can be found in the ext/ directory within the
PHP source or at the PECL link above.
This extension is only available if PHP was configured with
--with-dom[=DIR]. Add
--with-dom-xslt[=DIR] to include DOM
XSLT support. DIR is the libxslt install directory. Add
--with-dom-exslt[=DIR] to include DOM
EXSLT support, where DIR is the libexslt install directory.
Windows users will enable php_domxml.dll inside
of php.ini in order to use these functions.
In PHP 4 this DLL resides in
the extensions/ directory within the PHP Windows
binaries download.
You may download this PECL
extension DLL from the
PHP Downloads page or at
http://snaps.php.net/.
Also, there is one additional DLL that must be made available to your
system's PATH in order for this extension to work. In PHP 4 this is
in the dlls/ directory. It's name:
For PHP <= 4.2.0, it's libxml2.dll.
For PHP >= 4.3.0, it's iconv.dll.
And as of PHP 5.0.0, iconv is compiled into your Windows PHP binaries by
default so no extra DLL is needed.
There are quite a few functions that do not fit into the DOM standard and
should no longer be used. These functions are listed in the following table.
The function DomNode_append_child() has changed its
behaviour. It now adds a child and not a sibling. If this
breaks your application, use the non-DOM function
DomNode_append_sibling().
Table 1. Deprecated functions and their replacements
The constants below are defined by this extension, and
will only be available when the extension has either
been compiled into PHP or dynamically loaded at runtime.
The API of the module follows the DOM Level 2 standard as closely
as possible. Consequently, the API is fully object-oriented.
It is a good idea to have the DOM standard available when
using this module.
Though the API is object-oriented, there are many functions which can
be called in a non-object-oriented way by passing the object to operate
on as the first argument. These functions are mainly to retain compatibility
to older versions of the extension, and should not be used when creating new
scripts.
This API differs from the official DOM API in two ways. First, all
class attributes are implemented as functions with the same name.
Secondly, the function names follow the PHP naming convention. This means
that a DOM function lastChild() will be written as last_child().
This module defines a number of classes, which are listed -
including their
method - in the following tables. Classes with an equivalent in the
DOM standard are named DOMxxx.
Table 3. List of classes
Class name | Parent classes |
---|
DomAttribute | DomNode |
DomCData | DomNode |
DomComment | DomCData : DomNode |
DomDocument | DomNode |
DomDocumentType | DomNode |
DomElement | DomNode |
DomEntity | DomNode |
DomEntityReference | DomNode |
DomProcessingInstruction | DomNode |
DomText | DomCData : DomNode |
Parser | Currently still called DomParser |
XPathContext | |
Table 4. DomDocument class (DomDocument : DomNode)
Table 5. DomElement class (DomElement : DomNode)
Table 7. DomAttribute class (DomAttribute : DomNode)
Table 8. DomProcessingInstruction class (DomProcessingInstruction : DomNode)
Table 9. Parser class
Method name | Function name | Remark |
---|
add_chunk | Parser_add_chunk() | |
end | Parser_end() | |
Table 10. XPathContext class
Method name | Function name | Remark |
---|
eval | XPathContext_eval() | |
eval_expression | XPathContext_eval_expression() | |
register_ns | XPathContext_register_ns() | |
Table 11. DomDocumentType class (DomDocumentType : DomNode)
The classes DomDtd is derived from DomNode. DomComment is derived from
DomCData.
Many examples in this reference require an XML string. Instead of
repeating this string in every example, it will be put into a file
which will be included by each example. This include file is shown in the
following example section. Alternatively, you could create an XML document and
read it with DomDocument_open_file().
Example 1. Include file example.inc with XML string
<?php $xmlstr = "<?xml version='1.0' standalone='yes'?> <!DOCTYPE chapter SYSTEM '/share/sgml/Norman_Walsh/db3xml10/db3xml10.dtd' [ <!ENTITY sp \"spanish\"> ]> <!-- lsfj --> <chapter language='en'><title language='en'>Title</title> <para language='ge'> &sp; <!-- comment --> <informaltable ID='findme' language='&sp;'> <tgroup cols='3'> <tbody> <row><entry>a1</entry><entry morerows='1'>b1</entry><entry>c1</entry></row> <row><entry>a2</entry><entry>c2</entry></row> <row><entry>a3</entry><entry>b3</entry><entry>c3</entry></row> </tbody> </tgroup> </informaltable> </para> </chapter>"; ?>
|
|