[ACCEPTED]-PHP5: Find Root Node in DOMDocument-root-node

Accepted answer
Score: 68

DOMElement extends DOMNode.

You get the Root DOMElement by $d->documentElement.


Score: 17

DOM Model- The W3C has broken down the DOM 5 into a tree structure of nodes of varying 4 types. The Node interface is the base interface 3 for all elements. All objects implementing 2 this interface expose methods for dealing 1 with children.

$dom=new DomDocument;
$root=$dom->documentElement; // Root node
Score: 5

According to the PHP docs DOMElement is a subclass of DOMNode, so it should 1 inherit the hasChildNodes()-method.

Score: 0

Prior to php 5.1.3 this guy has it licked


 * function getXMLRootNode
 * @param string An xml string
 * @return string Return XML root node name

function getXMLRootNode($xmlstr)
 // Create DOM model
 $doc = new DOMDocument();

 // Load the XML string
 throw new Exception('Unable to parse XML string');

 // Find the root tag name
 $root = $doc->documentElement;

 throw new Exception('Unable to find XML root node');

 throw new Exception('Unable to find XML root node name');

 return $root->nodeName;

Cross 2 Posted to SO Questions that I hit while 1 trying to find how to do this pre 5.1.3

More Related questions