PHP: DOM Functions - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links | my php.net 
search for in the  
<scandirDOMAttr->isId>
view the version of this page
Last updated: Thu, 15 Jul 2004

XXIV. DOM Functions

�vod

The DOM extension is the replacement for the domxml extension from PHP 4. 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.

P�eddefinovan� konstanty

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.

Tabulka 1. XML constants

ConstantValueDescription
XML_ELEMENT_NODE (integer) 1Node is an element
XML_ATTRIBUTE_NODE (integer) 2Node is an attribute
XML_TEXT_NODE (integer) 3Node is a piece of text
XML_CDATA_SECTION_NODE (integer) 4 
XML_ENTITY_REF_NODE (integer) 5 
XML_ENTITY_NODE (integer) 6Node is an entity like &nbsp;
XML_PI_NODE (integer) 7Node is a processing instruction
XML_COMMENT_NODE (integer) 8Node is a comment
XML_DOCUMENT_NODE (integer) 9Node is a document
XML_DOCUMENT_TYPE_NODE (integer) 10 
XML_DOCUMENT_FRAG_NODE (integer) 11 
XML_NOTATION_NODE (integer) 12 
XML_HTML_DOCUMENT_NODE (integer) 13 
XML_DTD_NODE (integer) 14 
XML_ELEMENT_DECL_NODE (integer) 15 
XML_ATTRIBUTE_DECL_NODE (integer) 16 
XML_ENTITY_DECL_NODE (integer) 17 
XML_NAMESPACE_DECL_NODE (integer) 18 
XML_ATTRIBUTE_CDATA (integer) 1 
XML_ATTRIBUTE_ID (integer) 2 
XML_ATTRIBUTE_IDREF (integer) 3 
XML_ATTRIBUTE_IDREFS (integer) 4 
XML_ATTRIBUTE_ENTITY (integer) 5 
XML_ATTRIBUTE_NMTOKEN (integer) 7 
XML_ATTRIBUTE_NMTOKENS (integer) 8 
XML_ATTRIBUTE_ENUMERATION (integer) 9 
XML_ATTRIBUTE_NOTATION (integer) 10 

Tabulka 2. DOMException constants

ConstantValueDescription
DOM_INDEX_SIZE_ERR (integer) 1 
DOMSTRING_SIZE_ERR (integer) 2 
DOM_HIERARCHY_REQUEST_ERR (integer) 3 
DOM_WRONG_DOCUMENT_ERR (integer) 4 
DOM_INVALID_CHARACTER_ERR (integer) 5 
DOM_NO_DATA_ALLOWED_ERR (integer) 6 
DOM_NO_MODIFICATION_ALLOWED_ERR (integer) 7 
DOM_NOT_FOUND_ERR (integer) 8 
DOM_NOT_SUPPORTED_ERR (integer) 9 
DOM_INUSE_ATTRIBUTE_ERR (integer) 10 
DOM_INVALID_STATE_ERR (integer) 11 
DOM_SYNTAX_ERR (integer) 12 
DOM_INVALID_MODIFICATION_ERR (integer) 13 
DOM_NAMESPACE_ERR (integer) 14 
DOM_INVALID_ACCESS_ERR (integer) 15 
DOM_VALIDATION_ERR (integer) 16 

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.

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 nameParent classes
DOMAttrDOMNode
DOMCDataSectionDOMText : DOMNode
DOMCharacterDataDOMNode
DOMCommentDOMCharacterData : DomNode
DOMDocumentDOMNode
DOMDocumentFragmentDOMNode
DOMDocumentTypeDOMNode
DOMElementDOMNode
DOMEntityDOMNode
DOMEntityReferenceDOMNode
DOMNode 
DOMNotationDOMNode
DOMProcessingInstructionDOMNode
DOMTextDOMCDataSection : DomNode
DOMException 
DOMImplementation 
DOMNamedNodeMap 
DOMNodeList 
DOMXPath 

Obsah
DOMAttr->isId --  Checks if attribute is a defined ID
DOMCharacterData->appendData --  Append the string to the end of the character data of the node.
DOMCharacterData->deleteData --  Remove a range of characters from the node.
DOMCharacterData->insertData --  Insert a string at the specified 16-bit unit offset.
DOMCharacterData->replaceData --  Replace a substring within the DOMCharacterData node.
DOMCharacterData->substringData --  Extracts a range of data from the node.
DOMDocument->createAttribute -- Create new attribute
DOMDocument->createAttributeNS --  Create new attribute node with an associated namespace
DOMDocument->createCDATASection -- Create new cdata node
DOMDocument->createComment -- Create new comment node
DOMDocument->createDocumentFragment -- Create new document fragment
DOMDocument->createElement -- Create new element node
DOMDocument->createElementNS --  Create new element node with an associated namespace
DOMDocument->createEntityReference -- Create new entity reference node
DOMDocument->createProcessingInstruction -- Creates new PI node
DOMDocument->createTextNode -- Create new text node
DOMDocument->getElementById -- Searches for an element with a certain id.
DOMDocument->getElementsByTagName -- Searches for all elements with given tag name.
DOMDocument->getElementsByTagNameNS --  Searches for all elements with given tag name in specified namespace.
DOMDocument->importNode -- Import node into current document.
DOMDocument->load --  Load XML from a file.
DOMDocument->loadHTML --  Load HTML from a string.
DOMDocument->loadHTMLFile --  Load HTML from a file.
DOMDocument->loadXML --  Load XML from a string.
DOMDocument->normalize --  Normalizes document.
DOMDocument->relaxNGValidate --  Performs relaxNG validation on the document.
DOMDocument->relaxNGValidateSource --  Performs relaxNG validation on the document.
DOMDocument->save --  Dumps the internal XML tree back into a file
DOMDocument->saveHTML --  Dumps the internal document into a string using HTML formatting
DOMDocument->saveHTMLFile --  Dumps the internal document back into a file using HTML formatting
DOMDocument->saveXML -- Dumps the internal XML tree back into a string
DOMDocument->schemaValidate --  Validates a document based on a schema.
DOMDocument->schemaValidateSource --  Validates a document based on a schema.
DOMDocument->validate --  Validates the document based on its DTD.
DOMDocument->xinclude --  Substitutes XIncludes in a DOMDocument Object.
DOMElement->getAttribute -- Returns value of attribute
DOMElement->getAttributeNode -- Returns attribute node
DOMElement->getAttributeNodeNS --  Returns attribute node
DOMElement->getAttributeNS -- Returns value of attribute
DOMElement->getElementsByTagName -- Gets elements by tagname
DOMElement->getElementsByTagNameNS -- Get elements by namespaceURI and localName
DOMElement->hasAttribute -- Checks to see if attribute exists
DOMElement->hasAttributeNS --  Checks to see if attribute exists
DOMElement->removeAttribute -- Removes attribute
DOMElement->removeAttributeNode -- Removes attribute
DOMElement->removeAttributeNS -- Removes attribute
DOMElement->setAttribute -- Adds new attribute
DOMElement->setAttributeNode -- Adds new attribute node to element
DOMElement->setAttributeNodeNS -- Adds new attribute node to element
DOMElement->setAttributeNS -- Adds new attribute
DOMImplementation->createDocument --  Creates a DOM Document object of the specified type with its document element.
DOMImplementation->createDocumentType --  Creates an empty DOMDocumentType object.
DOMImplementation->hasFeature --  Test if the DOM implementation implements a specific feature and version.
DOMNamedNodeMap->getNamedItem --  Retrieves a node specified by name.
DOMNamedNodeMap->getNamedItemNS --  Retrieves a node specified by local name and namespace URI.
DOMNamedNodeMap->item -- Retrieves a node specified by index.
DOMNode->appendChild --  Adds new child at the end of the children
DOMNode->cloneNode --  Clones a node
DOMNode->hasAttributes --  Checks if node has attributes
DOMNode->hasChildNodes --  Checks if node has children
DOMNode->insertBefore --  Adds new child at the end of the children
DOMNode->isSameNode --  Indicates if two nodes are the same node.
DOMNode->isSupported --  Checks if feature is supported for specified version.
DOMNode->lookupNamespaceURI --  Returns namespace URI of the node based on the prefix.
DOMNode->lookupPrefix --  Returns name space prefix of the node based on namespaceURI.
DOMNode->normalize --  Normalizes the node.
DOMNode->removeChild --  Removes child from list of children
DOMNode->replaceChild --  Replaces a child
DOMNodelist->item --  Retrieves a node specified by index.
DOMText->isWhitespaceInElementContent --  Indicates whether this text node contains whitespace.
DOMText->splitText --  Breaks this node into two nodes at the specified offset.
DOMXPath->query --  Evaluates the XPath expression in the given string
DOMXPath->registerNamespace --  Registers the namespace with the DOMXpath object.


add a note add a note User Contributed Notes
DOM Functions
checksum at checksum.net.nz
03-Nov-2004 02:36
An excellent way to get a nodes elements in php5:
( remembering to use item(n) if more than one node exists )
 
one node:
$node = $dom->getElementsByTagname("node");
   foreach( $node->item(0)->attributes as $items){
   print "$items->nodeName: $items->nodeValue";
   }

If you wanted to search a list of the same nodes:

n nodes:
foreach(  $dom->getElementsByTagname("node") as $item ) {
   foreach( $item->attributes as $attrib ){
   print "$attrib->nodeName: $attrib->nodeValue";
   }
}
franp at free dot fr
02-Nov-2004 10:23
Since there is a complete interoperability between a DOM object and the corresponding SimpleXML object, you may be tempted to interchange both in order to get the best of the two worlds to achieve a given task.

If so, be aware that syntax of DOM and SimpleXML are, unfortunatly, not interoperable in most cases.

Consider for example the syntax to return a nodelist in both DOM and SimpleXML (in the following example the root tag is supposed to be "<racine>"):

<?php

// DOM object method is "childNodes"

$doc = new DomDocument;
$doc->loadXML($str);
foreach (
$doc->documentElement->childNodes as $child)
           {
// do something }

// while SimpleXML method is "children()"

$sxml = simplexml_import_dom($doc);
foreach (
$sxml->racine->children() as $child)
           {
// do something }

?>

Nota :  There is certainly some reason the php5 team chose to not unify DOM and SimpleXML and I am not able, technicaly, to discuss that choice. It would just be nice if a comparative table of syntaxes/methodes in DOM and SimpleXML was made available somewhere. Even if not comparative, simply a table of all available methods in the DOM API and SimpleXML API would be of great help.
ultralen at hotmail dot com
27-Oct-2004 03:49
You can get the attributes of a node with the attributes method, e.g.:

$attribs = $node->attributes;
foreach( $attribs as $attrib )
   print $attrib->name . "=" . $attrib->value . "\n";

It works for me (php 5.0.1 on win2k). The method is documented in the w3c specs, but I can't find it in the php html docs.
markoKILLTHESPAM at rhein-neckar-guide dot de
12-Oct-2004 03:15
You must set:

DomDocument->substituteEntities  = true;

If you like entities replaced, else they are just lost (before loading the DOM).
j dot e dot r at gmx dot net
03-Oct-2004 07:40
// Get all attributes of an element as NodeList:

$attributeNodeList = $xpath->query( "//path/to/element/@*" );

for( $k=0; $k<$attributeNodeList->length; $k++ ){
   $attribute = $attributeNodeList->item($k);
   echo 'Attribute : ' . $attribute ->nodeName .' = '. $attribute ->firstChild->nodeValue ." <br> \n";
}
SG_01 Lunarchild
28-Sep-2004 04:03
Re: dn at NOSPAM dot xbe dot ch

- there seems to be no easy way get an array of all available attributes of a given node/element. i didn't find it here nor in the w3c dom2 specification.

---------------------

Here's a function that might help ya:

<?php

function GetElementArray($Node) {
 
$arrOut = Array();
 
$cNode = $Node->firstChild;
  while (
$cNode->nextSibling != NULL) {
  
$arrOut[] = $cNode;
  
$cNode = $cNode->nextSibling;
  }
  return
$arrOut;
}

?>

Also you might try using an xpath query and getting a nodelist which basicaly is an array ;)
dn at NOSPAM dot xbe dot ch
27-Sep-2004 01:31
the w3c stuff seems to be a little bad-concepted..

- there seems to be no easy way get an array of all available attributes of a given node/element. i didn't find it here nor in the w3c dom2 specification.
- the php documentation doesn't really cover ANY attributes which some objects have - and they are really important.

example if you wish to get an element and simply print out its content (with some error checking):
<?php
$el
= $xml->getElementsByTagName("tagname");
if(
count($el)<1) {
   return
false;
}else{
   foreach(
$el as $val) {
     echo
$val->firstChild->nodeValue;
   }
}
?>

as you see, you have to go always through firstChild and it's attribute nodeValue to get the desired value

as mentioned, the attributes of each object type should be documented as they're very important. i would document them if i would know them....... ;)
jw at jwscripts dot com
17-Sep-2004 04:21
The following shows how to add a DTD to an XML document:

<?php

// Create an instance of the DomImplementation class
$impl = new DomImplementation;

// Create an instance of the DomDocumentType class by
// calling the DomImplementation::createDocumentType() method
// (arguments: doctype_name, doctype_public, doctype_system)
$dtd = $impl->createDocumentType("graph", "", "graph.dtd");

// Create an instance of the DomDocument class by
// calling the DomImplementation::createDocument() method
// (arguments: NS_URL, NS_PREFIX, DomDocumentType_instance)
$dom = $impl->createDocument("", "", $dtd);

// Set other properties
$dom->encoding = "UTF-8";
$dom->standalone = "no";

// Create an empty element
$element = $dom->createElement("graph");

// Append the element
$dom->appendChild($element);

// Retrieve and print the document
print $dom->saveXML());

/*
 Result:
 =========
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <!DOCTYPE graph SYSTEM "graph.dtd">
 <graph/>
*/

?>
dave at guidedvision dot com
31-Aug-2004 04:17
To load external entities from a doctype declaration set the member resolveExternals to true. This is useful for including character entities in your custom xml documents.

For example:
<?php
$xml
= <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE page SYSTEM "">
<page>&nbsp; &oelig; &eacute;</page>
XML;                                                                                                                           
$dom = new domDocument();
$dom->resolveExternals = true;
$dom->loadXML($xml);
echo
$dom->saveXML();                                                                                                                         
?>

Without setting resolveExternals to true the "&nbsp; &oelig; &eacute;" are lost.
socalsam at bigfoot dot com
29-Aug-2004 12:05
I think it will save many people a few hours if it was made clear that there is not a "document" object available for PHP. When I say "document" object, I mean the DOM object called "document". This is because the HTML is still incomplete when PHP executes, correct?

If any of this is not accurate, then certainly edit this as appropriate.
gem at rellim dot com
05-Aug-2004 11:32
Here is a guick link for those in a hurry to find the DOM2 standards:

  
zombie(at)localm(dot)org
21-Jul-2004 05:11
Here is just a quick note for people migrating to PHP5 dom functions from PHP4 domxml functions.

Here is a really quick snippet of doing the "same ol' stuff".
--
$doc = new DOMDocument;
$doc->loadXML($some_xml_string);
$root_node = $doc->documentElement;
$root_xml_string = $doc->saveXML($root_node);
--

Basically, we just create a dom document of a string of XML.
Then we gank the root node and dump it out as a string.

Notice, dom_xml_open_mem is deprecated as stated above. The replacement is DomDocument::loadXML(string)

Also, domdocument->document_element() is no longer. It is now a member variable- documentElement. This is not in the documentation here that i could see.

Finally, no more domdocument->dump_node(domnode); DomDocument::saveXML(DomElement) is the new replacement.

Hope that helps someone some time.

Blaine Garrett
d-wetzel at gmx dot net
28-Jun-2004 07:53
Use 'nodeType' to get the type of a node. Returns value of the predefined XML constants listed above.

Example:

- You can use the following xml-structure (name it structure.xml):

<?xml version="1.0"?>
<foo><bar><text>DOM in PHP5 is good.</text><text>Hello.</text></bar></foo>

- The code:

<?php

$document
= new DomDocument();
$document->preserveWhiteSpace = false;
$document->load("structure.xml");

$xpath = new domXPath($document);
$xpathQuery = $xpath->query("/foo/bar/text[position()=1]");
      
foreach (
$xpathQuery as $nodeList)
{
   if(
$nodeList->firstChild->nodeType==XML_TEXT_NODE) echo "Node is a piece of text.<br />";
}

?>

Dominikus Wetzel

<scandirDOMAttr->isId>
 Last updated: Thu, 15 Jul 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This unofficial mirror is operated at: /
Last updated: Sun Nov 14 23:09:54 2004 Local time zone must be set--see zic manual page