Mid-Atlantic Developer Conference - Call for Speakers


(PHP 4 >= 4.1.0)

DomDocument->document_element Returns root element node


domelement DomDocument->document_element ( void )

This function returns the root element node of a document.

The following example returns just the element with name CHAPTER and prints it. The other node -- the comment -- is not returned.

Example #1 Retrieving root element


if (!
$dom domxml_open_mem($xmlstr)) {
"Error while parsing the document\n";

$root $dom->document_element();

The above example will output:

domelement Object
    [type] => 1
    [tagname] => chapter
    [0] => 6
    [1] => 137960648

add a note add a note

User Contributed Notes 3 notes

jaworskidaniel no at spam gmail dot com
9 years ago
in php5

            $root = $dom->documentElement;
            $tag = $root->tagName;
filipp at mac dot com
11 years ago
as of PHP 5 (tested with 5.1.4), remember to use documentElement instead. As in:
$dom = new DomDocument ();
$dom -> load ('file.xml');
$newEl = $dom -> createElement ('newEl');
$dom -> documentElement -> appendChild ($newEl);
misterffoeg at hotmail dot com
10 years ago
The last note is incorrect. The class he is referring to is DOMDocument, not DomDocument. This page is for the PHP 4 extension. Doing "new DomDocument" would be a fatal error in PHP 5 unless you hacked the old extension into it which would be a very useless move.
To Top