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()
applied to it. Therefore, if you want to read an XML file and write
a modified version, use DomDocument_create_element(),
set_attribute(), etc. and finally the
This extension has been moved to the PECL repository and is no longer bundled with
PHP as of PHP 5.0.0.
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
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.
本 PECL 扩展未绑定于 PHP 中。
在 PHP 4 中本 PECL 扩展的源程序位于
PHP 源程序中的 ext/ 目录下或者在上面的
This extension is only available if PHP was configured with
--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.
在 PHP 4 中本 DLL 位于
PHP Windows 执行包中的 extensions/ 目录下。
下载此 PECL 扩展的
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
表格 1. Deprecated functions and their replacements
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
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 -
method - in the following tables. Classes with an equivalent in the
DOM standard are named DOMxxx.
表格 3. List of classes
|Class name||Parent classes|
|DomComment||DomCData : DomNode|
|DomText||DomCData : DomNode|
|Parser||Currently still called DomParser|
表格 4. DomDocument class (DomDocument : DomNode)
表格 5. DomElement class (DomElement : DomNode)
表格 7. DomAttribute class (DomAttribute : DomNode)
表格 8. DomProcessingInstruction class (DomProcessingInstruction : DomNode)
表格 9. Parser class
|Method name||Function name||Remark|
表格 10. XPathContext class
|Method name||Function name||Remark|
表格 11. DomDocumentType class (DomDocumentType : DomNode)
The classes DomDtd is derived from DomNode. DomComment is derived from
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().
例子 1. Include file example.inc with XML string
$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>
<!-- comment -->
<informaltable ID='findme' language='&sp;'>