|
|
XXVIII. DOM XML Functions
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 has been moved to the 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
DOM extension.
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 not bundled with PHP.
Additional information such as new releases,
downloads, source files, maintainer information, and a CHANGELOG, can be
located here:
.
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 .
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().
表格 1. Deprecated functions and their replacements
������Щ�����ɸ��չģ�鶨��ģ����ֻ���ڸ��չģ�鱻���뵽 PHP �У����������ʱ�̬���غ���Щ������Ч�
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.
表格 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 | |
表格 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 |
---|
add_chunk | Parser_add_chunk() | | end | Parser_end() | |
表格 10. XPathContext class Method name | Function name | Remark |
---|
eval | XPathContext_eval() | | eval_expression | XPathContext_eval_expression() | | register_ns | XPathContext_register_ns() | |
表格 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().
例子 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
steve at find-information-on dot com
12-Feb-2005 12:33
If you are using apache, instead of copying files around (iconv.dll for instance) you can use this in your httpd.conf for apache:
LoadFile "d:/php/dlls/iconv.dll"
I placed this line before
LoadModule php4_module "d:/php/sapi/php4apache2.dll"
and it worked, no copying of files or anything therefore helps when updating php, don't have to mess around searching for files and other stuff.
ndaniel at bsi-scs dot com
22-Dec-2004 01:54
I recently developed a script for parsing DHL XML transaction responses - finding it a pain in the rear to actually parse the XML and set my variables - it actually wasn't that hard once I figured it out - and it goes something like this...
<?php
function loop($node) {
if ($node->has_attributes()) {
$this->set_attributes($node);
} if ($node->has_child_nodes()) {
$this->loop($node->first_child());
} else {
$this->set_data($node);
} $node = $node->next_sibling();
if ($node) {
$this->loop($node);
} } ?>
The code goes from the root element, if the element has attributes - it sets attribute variables. Second, it recursively proceeds to the lowest level element (no more children). Once that level has been reached, data variables are set. The next step goes to the next sibling of the element, if it exists. If the next sibling does not exists, the function is ended and the current element is returned to the parent element. The parent element is then checked for siblings. This process continues (as is with recursion) until the parent element is back at the root element, which is the end of the document.
deborah dot seidman at imc-fr dot com
10-Dec-2004 04:15
You can always use a sax parser (expat) which saves on memory storage (there is none as sax is event driven) and use this neat code to produce an array structure of you xml file :
see
comment by
tgrabietz at bupnet dot de
22-Sep-2004 05:05
jas at uci dot edu
15-Nov-2004 11:56
i needed to have an easy way to create a multi-dimensional but EXTREMELY SIMPLE php array out of some XML text i'm receiving. NOT an object. just an ARRAY.
i found that as simple a request as this seemed to be, the new (php5) DOM functions do not provide this functionality.
even the SimpleXML functions are object-oriented, which doesn't work for some of my purposes (sending to a Smarty template variable for looping through, etc.) -- returning attributes as SimpleXMLElement objects instead of strings, etc.. i just wanted an ARRAY containing the data as STRINGS.
eli () had submitted such code earlier, based on domxml/php4 calls. his function was called "domxml_xmlarray".
but when php5 came out, eli's comments at the bottom of the PHP site got erased. (fortunately, i had already saved his code.) no doubt, mine will too w/next version..
furthermore, as far as i can tell, no one has taken the cue to add something like eli's domxml_xmlarray function directly into the DOMDocument object (but it would be nice).
so i translated eli's code, now using the dom calls (instead of the older domxml calls), and renamed the function to "dom_to_simple_array()".
below is a script containing the function itself as well as an example of its use. just copy it to your server somewhere and execute it and it should work right off the bat if you are using php5.
thanks.
jeff stern
==================================================================
<?php
function dom_to_simple_array($domnode, &$array) {
$array_ptr = &$array;
$domnode = $domnode->firstChild;
while (!is_null($domnode)) {
if (! (trim($domnode->nodeValue) == "") ) {
switch ($domnode->nodeType) {
case XML_TEXT_NODE: {
$array_ptr['cdata'] = $domnode->nodeValue;
break;
}
case XML_ELEMENT_NODE: {
$array_ptr = &$array[$domnode->nodeName][];
if ($domnode->hasAttributes() ) {
$attributes = $domnode->attributes ();
if (!is_array ($attributes)) {
break;
}
foreach ($attributes as $index => $domobj) {
$array_ptr[$index] = $array_ptr[$domobj->name] = $domobj->value;
}
}
break;
}
}
if ( $domnode->hasChildNodes() ) {
dom_to_simple_array($domnode, $array_ptr);
}
}
$domnode = $domnode->nextSibling;
}
}
$strXMLData = "<contacts>
<contact>
<name>
John Doe
</name>
<phone>
123-456-7890
</phone>
</contact>
<contact>
<name>
Mary Smiley
</name>
<phone>
567-890-1234
</phone>
</contact>
</contacts>";
$domdoc = new DOMDocument;
$domdoc->loadXML($strXMLData);
$aData = array();
dom_to_simple_array($domdoc, $aData);
?><html>
<body>
<p>there are <? echo count($aData['contacts'][0]['contact']); ?>
contacts</p>
<p>the 2nd contact's phone number is
<?echo $aData['contacts'][0]['contact'][1]['phone'][0]['cdata']; ?>
</p>
<hr />
<p>Here is the raw array structure:</p>
<pre>
<? print_r($aData); ?>
</pre>
</body>
</html>
==================================================================
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-
| |