|
|
XXV. DOM XML FunctionsVarov�n� | Toto roz���en� je
EXPERIMENT�LN�. Chov�n� tohoto roz���en�,
n�zvy funkc� a v�echno ostatn�, co je zde zdokumentov�no, se v budouc�ch
verz�ch PHP m��e bez ohl�en� zm�nit. Berte to v �vahu a pou��vejte tento modul
na vlastn� nebezpe��. |
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.
This extension makes use of the
. Download
and install this library. You will need at least libxml-2.4.14.
To use DOM XSLT features you can use the
and EXSLT
enhancements from .
Download and install these libraries if you plan to use (enhanced) XSLT
features. You will need at least libxslt-1.0.18.
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.
Note to Win32 Users:
In order to enable this module on a Windows environment, you must copy one
additional file from the DLL folder of the PHP/Win32 binary package to
the SYSTEM32 folder of your Windows machine (Ex: C:\WINNT\SYSTEM32 or
C:\WINDOWS\SYSTEM32). For PHP <= 4.2.0 copy
libxml2.dll, for PHP >= 4.3.0 copy
iconv.dll from the DLL folder to your SYSTEM32 folder.
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().
Tabulka 1. Deprecated functions and their replacements
Tyto konstanty jsou definov�ny t�mto roz���en�m a budou k dispozici pouze
tehdy, bylo-li roz���en� zkompilov�no spole�n� s PHP nebo dynamicky zavedeno
za b�hu.
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.
Tabulka 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 | |
Tabulka 4. DomDocument class (DomDocument : DomNode)
Tabulka 5. DomElement class (DomElement : DomNode)
Tabulka 7. DomAttribute class (DomAttribute : DomNode)
Tabulka 8. DomProcessingInstruction class (DomProcessingInstruction : DomNode)
Tabulka 9. Parser class Method name | Function name | Remark |
---|
add_chunk | Parser_add_chunk() | | end | Parser_end() | |
Tabulka 10. XPathContext class Method name | Function name | Remark |
---|
eval | XPathContext_eval() | | eval_expression | XPathContext_eval_expression() | | register_ns | XPathContext_register_ns() | |
Tabulka 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().
P��klad 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>";
?>
|
|
add a note
User Contributed Notes
DOM XML Functions
Alex
04-Jul-2004 04:48
PHP4/DOMXML code is not compatible with the new PHP5/dom extension. While the conversion is quite strait forward, it can take a long time if domxml has been broadly used. Moreover, it can be interesting to have old PHP4 scripts ready for PHP5 as soon as possible even if the server is still running PHP4. Since I have that kind of problem, if have written a small library to include in PHP4 scripts to enable them to be run on PHP5.
It does not cover all the domxml functionality, but most of the main functions and can easily be extended. Tested with PHP4.3.7 and PHP5.0.0RC3 but I will try to keep it updated. I hope it can help.
sn00b at yahoo dot com
23-Jun-2004 11:18
When you want to use XSLT extensions you have to copy "expat.dll" and "sablot.dll" into your windows-system directory.
closedfile
21-Jun-2004 05:01
If you use IIS and Windows NT, pleace make sure that the file
"iconv.dll" is in the directory "winnt/system32/". Else you will get an error.
SAM
22-Apr-2004 06:05
When installing PHP --with-dom and --with-dom-xslt on a Red Hat 9.0 remember to install the following packages:
libxml
libxml2
libxml2-devel
libxslt
libxslt-devel
Then you will be spared error messages when trying to configure.
regards
SAM
ngc(dontSpamMe)rapanden(SpamOFF)dk
28-Jan-2004 12:48
Sorry, a bug in my code... I made the first version late at night, sorry!
The bug was in the "if ($ChildDomNode->has_child_nodes())" block, I didn't save the data for the for the CildNode of the CildNodes. the bug has been fixed.
<?php
function getElementAttributes($DomNode,$elementName,$attriName)
{
if ($ChildDomNode = $DomNode->first_child())
{
while($ChildDomNode)
{
if ($ChildDomNode->node_type() == XML_ELEMENT_NODE)
{
if($ChildDomNode->node_name() == $elementName)
{
if ($ChildDomNode->has_attributes())
{
$Array = $ChildDomNode->attributes();
foreach ($Array AS $DomAttribute)
{
if($DomAttribute->name() == $attriName)
{
$nodeArray[] = $DomAttribute->value();
}
}}}
if ($ChildDomNode->has_child_nodes())
{
$tmpArray = (getElementAttributes($ChildDomNode,$elementName,$attriName));
$nodeArray = array_merge($nodeArray, $tmpArray);
unset($tmpArray);
}}$ChildDomNode = $ChildDomNode->next_sibling();
}return $nodeArray;
}}
$file = "test3.xml";
$element = "pb";
$att = "id";
$DomDocument = domxml_open_file($file);
$RootDomNode = $DomDocument->document_element();
$array = getElementAttributes($RootDomNode,$element,$att);
echo "<pre>";
print_r($array);
echo "</pre>";
?>
bps7j at yahoSPAMo.com
24-Dec-2003 08:19
I've also rolled my own DOM-like functionality for places where the DOM extensions aren't available.
bradparks at bradparks dot com
17-Dec-2003 10:41
Hey;
If you need to parse XML on an older version of PHP (e.g. 4.0) or if you can't get the expat extension enabled on your server, you might want to check out the Saxy and DOMIT! xml parsers from Engage Interactive. They're opensource and pure php, so no extensions or changes to your server are required. I've been using them for over a month on some projects with no problems whatsoever!
Check em out at:
DOMIT!, a DOM based xml parser, uses Saxy (included)
or
Saxy, a sax based xml parser
Brad
bart at mediawave dot nl
11-Nov-2003 07:46
This recursive function will iterate over a DOM object and display it as a nicely formatted XML structure. I used intuitive variable names to help learn more about the DOM functions and their return values.
<<?php
function PrintDomTree($DomNode)
{
if ($ChildDomNode = $DomNode->first_child()) {
static $depth = 0;
$whitespace = "\n<br>".str_repeat(" ", ($depth * 2));
while ($ChildDomNode) {
if ($ChildDomNode->node_type() == XML_TEXT_NODE) {
echo trim($ChildDomNode->node_value());
} elseif ($ChildDomNode->node_type() == XML_ELEMENT_NODE) {
$HasTag = 1;
echo $whitespace;
echo "<", $ChildDomNode->node_name();
if ($ChildDomNode->has_attributes()) {
$Array = $ChildDomNode->attributes();
foreach ($Array AS $DomAttribute) {
echo " ", $DomAttribute->name(), "=\"", $DomAttribute->value(), "\"";
}
}
echo ">";
if ($ChildDomNode->has_child_nodes()) {
$depth++;
if (PrintDomTree($ChildDomNode)) {
echo $whitespace;
}
$depth--;
}
echo "</", $ChildDomNode->node_name(), ">";
}
$ChildDomNode = $ChildDomNode->next_sibling();
}
return $HasTag;
}
}
?>
nospam at phppatterns dot com
09-Apr-2003 08:51
If you're having trouble understanding how the the DOM XML extension fits together you may find the UML diagram here helps:
sorn at dsl dot upc dot es
17-Mar-2003 11:38
When parsing "iso-8859-1" encoded XML files, use "utf8_decode" to recover node contents (libxml uses "UTF-8" internal encoding, so conversion needed).
--- BEGIN: mydata.xml ---
<?xml version="1.0" encoding="iso-8859-1"?>
...
--- END: mydata.xml---
--- BEGIN: myparser.php ---
<?php
...
$domxml = domxml_open_file("mydata.xml"));
...
$content = utf8_decode(trim($node->content));
echo $content;
...
?>
--- END: myparser.php
-eof-
Daniel dot Veillard at w3 dot org
12-Aug-2000 06:46
I'm libxml maintainer, I suggest anybody using
the DOM functions to:
- not stay with 2.0.x versions, upgrade please
- have a look at xmlsoft.org if you want more
information, but this might be misleading too
since PHP-DOM doesn't expose all the
libxml API
- for the DOM reference
- if you have trouble when parsing an
XML/HTML files, check first that they are
correct (xmllint distributed with libxml may
help), if no fix them, if yes send me a bug
report with the input exhibiting the problem
happy DOM hacking,
[email protected]
| |