Get Root Node using DOMDocument in PHP 5

I am trying to get the root node of this XML using DOMDocument in PHP 5.

<diagnosis xmlns="http://checkout.google.com/schema/2" serial-number="8088c57b-bf00-4e83-b2d7-df9767da9140">
  <input-xml>
    <checkout-shopping-cart>
      <shopping-cart>
      </shopping-cart>
      <checkout-flow-support>
      </checkout-flow-support>
    </checkout-shopping-cart>
  </input-xml>
</diagnosis>

The root of the document should be "diagnosis" my code looks like this:
$dom_response = new DOMDocument();
    $boolresult = $dom_response->loadXML($diagnose_response);
    $root_element = $dom_response->getDocumentElement;
    $root_tag = $root_element->tagname;

The loadXML function returns TRUE, but when I call the property getDocumentElement it returns NULL.  

How would I go about getting the document root of the XML supplied?
 
yami_riderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roger BaklundCommented:
The name of the properties are documentElement and tagName (case sensitive):
    $dom_response = new DOMDocument();
    $boolresult = $dom_response->loadXML($diagnose_response);
    $root_element = $dom_response->documentElement;
    $root_tag = $root_element->tagName;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
No disrespect meant to the DOM, but this will do it with SimpleXML -  var_dump() works on the object, which makes debugging easier.
<?php // RAY_DOM_root_node.php
error_reporting(E_ALL);
echo "<pre>\n"; // READABILITY
 
 
// DATA FROM THE OP
$xml = '<diagnosis xmlns="http://checkout.google.com/schema/2" serial-number="8088c57b-bf00-4e83-b2d7-df9767da9140">
  <input-xml>
    <checkout-shopping-cart>
      <shopping-cart>
      </shopping-cart>
      <checkout-flow-support>
      </checkout-flow-support>
    </checkout-shopping-cart>
  </input-xml>
</diagnosis>';
 
// MAKE AN OBJECT AND VISUALIZE IT
$obj = SimpleXML_Load_String($xml);
// var_dump($obj);
 
// PREPEND THE CORRECT XML HEADERS
$new_xml = '<?xml version="1.0" ?><wrapper>' . $xml . '</wrapper>';
 
// MAKE AN OBJECT AND VISUALIZE IT
$new_obj = SimpleXML_Load_String($new_xml);
// var_dump($new_obj);
 
// MAKE AN ITERATOR TO EXTRACT THE TOP KEY
foreach ($new_obj as $key => $thing)
{
   var_dump($key);
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.