3

I have a DomDocument, which is impossible to var_dump (it really makes me angry) on which I do :

var_dump($dom->getElementsByTagName('url'));

on a response like this :

<?xml version="1.0" ?>
<rss version="2.0">
  <channel>
      <url>beaute-mode/cheveux/11460--choisir-un-headband</url>
      <title>get_url_article</title>
      <host>myhost</host>
  </channel>
</rss> 

But I got the var_dump echoing : object(DOMNodeList)[262] instead of my raw data (which is an URL).

So my question is pretty simple but how do I get my raw data without being encapsulated in an DomNodeList ?

thanks.

sf_tristanb
  • 8,725
  • 17
  • 74
  • 118

2 Answers2

3

DOMNodeList::item() will give a DOMNode in the DOMNodeList by index. For example, to get the first node in the list:

$dom->getElementsByTagName('url')->item(0)

To get the actual data that the element contains, use code something like this:

if ($node = $dom->getElementsByTagName('url')->item(0)) {
  $url = $node->nodeValue;
} else {
  // Element does not exist, handle error here
}
DaveRandom
  • 87,921
  • 11
  • 154
  • 174
1

PHP itself - even as there were announced improvements with PHP 5.4 - is not that good in displaying structural information for DomNode and DomNodeList.

You can do something that I have outlined in a similar question Debug a DOMDocument Object in PHP:

$doc = new DOMDocument();
$doc->loadXML($xml);
DomTree::dump($doc->getElementsByTagName('url'));

Which will output the following:

`<url>
  `"beaute-mode/cheveux/11460--choisir-un-headband"

You can find the source-code in this Gist of mine.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Verry interesting answer, i'll bookmark your gist. PS : I'm using 5.4.6 and i don't see any differences between 5.3 with var_dump(DOMDocument) – sf_tristanb Aug 24 '12 at 11:43
  • I tested with PHP 5.4 now and I do not see them either. Looks like this needs more research and better understanding of the http://php.net/ChangeLog-5.php : Version 5.3.11; 26-Apr-2012 : DOM - Added debug info handler to DOM objects. – hakre Aug 24 '12 at 11:47