DOM XML Функции

Deprecated functions

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().

Deprecated functions and their replacements
Old function New function
xmldoc domxml_open_mem()
xmldocfile domxml_open_file()
domxml_new_xmldoc domxml_new_doc()
domxml_dump_mem DomDocument_dump_mem()
domxml_dump_mem_file DomDocument_dump_file()
DomDocument_dump_mem_file DomDocument_dump_file()
DomDocument_add_root DomDocument_create_element() followed by DomNode_append_child()
DomDocument_dtd DomDocument_doctype()
DomDocument_root DomDocument_document_element()
DomDocument_children DomNode_child_nodes()
DomDocument_imported_node No replacement.
DomNode_add_child Create a new node with e.g. DomDocument_create_element() and add it with DomNode_append_child().
DomNode_children DomNode_child_nodes()
DomNode_parent DomNode_parent_node()
DomNode_new_child Create a new node with e.g. DomDocument_create_element() and add it with DomNode_append_child().
DomNode_set_content Create a new node with e.g. DomDocument_create_text_node() and add it with DomNode_append_child().
DomNode_get_content Content is just a text node and can be accessed with DomNode_child_nodes().
DomNode_set_content Content is just a text node and can be added with DomNode_append_child().

Classes

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.

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  

DomDocument class (DomDocument : DomNode)
Method name Function name Remark
doctype DomDocument_doctype()  
document_element DomDocument_document_element()  
create_element DomDocument_create_element()  
create_text_node DomDocument_create_text_node()  
create_comment DomDocument_create_comment()  
create_cdata_section DomDocument_create_cdata_section()  
create_processing_instruction DomDocument_create_processing_instruction()  
create_attribute DomDocument_create_attribute()  
create_entity_reference DomDocument_create_entity_reference()  
get_elements_by_tagname DomDocument_get_elements_by_tagname()  
get_element_by_id DomDocument_get_element_by_id()  
dump_mem DomDocument_dump_mem() not DOM standard
dump_file DomDocument_dump_file() not DOM standard
html_dump_mem DomDocument_html_dump_mem() not DOM standard
xpath_init xpath_init not DOM standard
xpath_new_context xpath_new_context not DOM standard
xptr_new_context xptr_new_context not DOM standard

DomElement class (DomElement : DomNode)
Method name Function name Remark
tagname DomElement_tagname()  
get_attribute DomElement_get_attribute()  
set_attribute DomElement_set_attribute()  
remove_attribute DomElement_remove_attribute()  
get_attribute_node DomElement_get_attribute_node()  
set_attribute_node DomElement_set_attribute_node()  
get_elements_by_tagname DomElement_get_elements_by_tagname()  
has_attribute DomElement_has_attribute()  

DomNode class
Method name Remark
DomNode_node_name()  
DomNode_node_value()  
DomNode_node_type()  
DomNode_last_child()  
DomNode_first_child()  
DomNode_child_nodes()  
DomNode_previous_sibling()  
DomNode_next_sibling()  
DomNode_parent_node()  
DomNode_owner_document()  
DomNode_insert_before()  
DomNode_append_child()  
DomNode_append_sibling() Not in DOM standard. This function emulates the former behaviour of DomNode_append_child().
DomNode_remove_child()  
DomNode_has_child_nodes()  
DomNode_has_attributes()  
DomNode_clone_node()  
DomNode_attributes()  
DomNode_unlink_node() Not in DOM standard
DomNode_replace_node() Not in DOM standard
DomNode_set_content() Not in DOM standard, deprecated
DomNode_get_content() Not in DOM standard, deprecated
DomNode_dump_node() Not in DOM standard
DomNode_is_blank_node() Not in DOM standard

DomAttribute class (DomAttribute : DomNode)
Method name Remark
name DomAttribute_name()  
value DomAttribute_value()  
specified DomAttribute_specified()  

DomProcessingInstruction class (DomProcessingInstruction : DomNode)
Method name Function name Remark
target DomProcessingInstruction_target()  
data DomProcessingInstruction_data()  

Parser class
Method name Function name Remark
add_chunk Parser_add_chunk()  
end Parser_end()  

XPathContext class
Method name Function name Remark
eval XPathContext_eval()  
eval_expression XPathContext_eval_expression()  
register_ns XPathContext_register_ns()  

DomDocumentType class (DomDocumentType : DomNode)
Method name Function name Remark
name DomDocumentType_name()  
entities DomDocumentType_entities()  
notations DomDocumentType_notations()  
public_id DomDocumentType_public_id()  
system_id DomDocumentType_system_id()  
internal_subset DomDocumentType_internal_subset()  

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>"
;
?>

Содержание

Коментарии

If you're having trouble understanding how the the DOM XML extension fits together you may find the UML diagram here helps: http://www.phppatterns.com/index.php/article/articleview/38
2003-04-09 16:51:05
http://php5.kiev.ua/manual/ru/ref.domxml.html
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;
    }
}

?>
2003-11-11 13:46:06
http://php5.kiev.ua/manual/ru/ref.domxml.html
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)
http://www.engageinteractive.com/redir.php?resource=3&target=domit

or

Saxy, a sax based xml parser
http://www.engageinteractive.com/redir.php?resource=4&target=saxy

Brad
2003-12-17 16:41:27
http://php5.kiev.ua/manual/ru/ref.domxml.html
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 http://fr2.php.net/manual/fr/function.xml-parse.php

comment by 
tgrabietz at bupnet dot de
22-Sep-2004 05:05
2004-12-10 10:15:26
http://php5.kiev.ua/manual/ru/ref.domxml.html
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

// Use with a class containing functions set_attributes() and 
// set_data().  Use the following to set variables from the 
// resulting xml.  $node is a dom xml object - in the first call 
// to loop, $node would be equal to the root document 
// element.

function loop($node) {
 
// set attribute tags here
 
if ($node->has_attributes()) {
   
$this->set_attributes($node);
 } 
// end if node has attributes
       
 
if ($node->has_child_nodes()) {
   
$this->loop($node->first_child());
 } 
// end if node has child
 
else {
   
$this->set_data($node);
 } 
// end if node has no child
 // get next sibling
 
$node $node->next_sibling();
 if (
$node) {
   
$this->loop($node);
 } 
// end if node
// end function loop
?>

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.
2004-12-22 07:54:46
http://php5.kiev.ua/manual/ru/ref.domxml.html
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.
2005-02-11 18:33:54
http://php5.kiev.ua/manual/ru/ref.domxml.html
Hi at All,

if you use xpath_eval() you get a xpathobject with a type-member-variable, which tells you about the type of the found content. Here are the values and the corresponding types:
1 = XPATH_NODESET (integer)
2 = XPATH_BOOLEAN (integer)
3 = XPATH_NUMBER (integer)
4 = XPATH_STRING (integer)

I think, but don't know, that the rest of the constants are:
0 = XPATH_UNDEFINED (integer)     
5 = XPATH_POINT (integer)     
6 = XPATH_RANGE (integer)     
7 = XPATH_LOCATIONSET (integer)

I hope i could help some people.

Greetz,
Chris
2007-04-04 10:46:18
http://php5.kiev.ua/manual/ru/ref.domxml.html
The PHP 5.0 DOM is very powerful to create and work on valid RSS Feeds. I found a easy to follow step-by-step-tutorial here: 
http://xml-rss.de/xml-rss-feed-mit-php.htm
(in german language but the code can be understand in every language i think) maybe it helps someone to understand the DOM-Thing better. But beware it don´t work on PHP 4!
2008-07-18 07:55:43
http://php5.kiev.ua/manual/ru/ref.domxml.html

    Поддержать сайт на родительском проекте КГБ