sajan Posted November 21, 2007 Share Posted November 21, 2007 Hi How get the root node of an xml file .(Object created from DOMDocument).When i use the root() nothing output. eg. $xml=new DOMDocument();$xml->load($file_name); echo $xml->root(); i use php5. Link to comment Share on other sites More sharing options...
boen_robot Posted November 21, 2007 Share Posted November 21, 2007 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 More sharing options...
sajan Posted November 22, 2007 Author Share Posted November 22, 2007 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 More sharing options...
sajan Posted November 22, 2007 Author Share Posted November 22, 2007 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. Link to comment Share on other sites More sharing options...
boen_robot Posted November 22, 2007 Share Posted November 22, 2007 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.