15

I have a PHP5 DOMDocument and I try to find the root node (not the root element).

Example:

<test>
    <element>
        <bla1>x</bla1>
        <bla2>x</bla2>
    </element>
    <element>
        <bla1>y</bla1>
        <bla2>y</bla2>
    </element>
    <element>
        <bla1>z</bla1>
        <bla2>z</bla2>
    </element>
</test>

I want to get the DOMNode of "test" so that I can call - for example - hasChildNodes. I can get the "documentElement", but that's a DOMElement. Maybe I can go from there?

$d = DOMDocument::loadXML($xml);
// [... do some stuff here to find document's root node ...]
if ($rootnode->hasChildNodes()) echo 'yayy!'

Who can fill the gap? I seem to be blind.

(Obviously it's not only hasChildNodes I want to call - so NO, it doesn't help to find another method to find out if the document contains stuff. That's just for my simple example. I need a DOMNode at the end.)

BlaM
  • 28,465
  • 32
  • 91
  • 105
  • Okay, the problem seems to be the total confusion between "DOMDocument" and "DomDocument" between PHP5 and PHP4 and the weird documentation of it. I think I have it fixed now. Thanks everybody. – BlaM Jul 30 '09 at 10:45

4 Answers4

68

DOMElement extends DOMNode.

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

Cristian Toma
  • 5,662
  • 2
  • 36
  • 43
  • 12
    This should actually be the ticked answer! – Angel.King.47 Aug 30 '11 at 08:45
  • 2
    here is the documentation for this property http://www.php.net/manual/en/class.domdocument.php#domdocument.props.documentelement – gawpertron Sep 20 '12 at 12:55
  • I'll save someone a click. The above documentation says: `This is a convenience attribute that allows direct access to the child node that is the document element of the document.` – Ben Jan 24 '14 at 23:34
17

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

$dom=new DomDocument;
$dom->Load("file.xml");
$root=$dom->documentElement; // Root node
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
5

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

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
PatrikAkerstrand
  • 45,315
  • 11
  • 79
  • 94
  • Hmmm, okay, that's actually true... And if I don't send my ->documentElement to a function, everything seems to work correctly... The function call looses something on it's way... Now on to find that... – BlaM Jul 30 '09 at 10:38
0

Prior to php 5.1.3 this guy has it licked

https://macfoo.wordpress.com/2009/06/03/getting-the-root-node-from-an-xml-string

/**
 * 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
 if(!$doc->loadXML($xmlstr))
 {
 throw new Exception('Unable to parse XML string');
 }

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

 if(!isset($root))
 {
 throw new Exception('Unable to find XML root node');
 }

 if(!isset($root->nodeName))
 {
 throw new Exception('Unable to find XML root node name');
 }

 return $root->nodeName;
}

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

Cliffordlife
  • 488
  • 5
  • 13