Jump to content

How get root node of an xml file


Recommended Posts

Look at the PHP DOM reference. There isn't a root() method to the DOMDocument class. There is a documentElement property though, so you can get the root element object with

$xml=new DOMDocument();$xmlDoc->load($file_name);echo $xml->documentElement;

But with this, you again output nothing, as what you have returned now is a DOMElement object, not a string you can echo. If you want, you can get the whole contents of the file as a string with the textContent property of the root element like:

$xml=new DOMDocument();$xmlDoc->load($file_name);echo $xml->documentElement->textContent;

Link to comment
Share on other sites

Through your mehod

echo $xml->documentElement->textContent;

i get the whole text content.,But note the root tag.suppose i have an xml file

<?xml version="1.0"?><user><heading>Edit </heading><delete>Delete</delete><groupname>Name</groupname></user>

How i get the root node,here user?

Link to comment
Share on other sites

I got root node.The method i used
$xml=new DOMDocument();$xml->load($file_name);echo $xml->documentElement->tagName;

Also thanks boen for take interest in this topic.

Ahhh... so you meant the name of the root element. Next time, be more concrete about what you really want, as in DOM, when you say you want to get an element or an attribute, you are usually referring to the object representing that element or attribute, not it's name and/or contents and/or descendants.What you have used is the tagName property. A method is practically a funciton from a class (though I too have often been confused by the ambiguity between "method" as in "a function", and a "method" as "an approach"), like load() in the above case.
Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...