Accessing Nodes
domaccessingnodes.php:
<?php
$dom = new DomDocument();
$dom->load("examples/books.xml");
//gets the root element
$root = $dom->documentElement;
print "--- get all childnodes of the root nodes --- \n";
foreach ($root->childNodes as $child) {
print $child->nodeName ."\n";
}
print "--- get firstChild of root Node ---\n";
print $root->firstChild->nodeName ."\n";
print "--- get lastChild of rootNode --- \n";
print $root->lastChild->nodeName ."\n";
print "--- get nextSibling of firstChild of rootNode --- \n";
print $root->firstChild->nextSibling->nodeName ."\n";
print "--- get parentNode of firstChild of rootNode --- \n";
print $root->firstChild->parentNode->nodeName ."\n";
print "--- get all attribute of the first book node --- \n";
$firstBook = $root->firstChild->nextSibling->nextSibling->nextSibling;
foreach ($firstBook->attributes as $child) {
print $child->nodeName ." => " . $child->nodeValue ."\n";
}
print "--- get value of attribute id ---\n";
print $firstBook->getAttribute("id");
print "--- get ownerDocument of rootNode ---\n";
var_dump( $root->ownerDocument);
Accessing Nodes
© copyright 2004 Bitflux GmbH