Object of class DOMElement could not be converted to string

Raphaël picture Raphaël · Oct 4, 2011 · Viewed 27k times · Source

I try to parse an XML rss flux. Actually, an error is thrown:

Catchable fatal error: Object of class DOMElement could not be converted to string in ...

I want to get the value "test" of the tag "link"

Here is my code :

//check if url contents xml
            $content = file_get_contents($flux);

            $xml = new DOMDocument;
            $xml->loadXML($content);

            //get the link
            $link = $xml->getElementsByTagName('link')->item(0);

            echo $link;

Here is the flux :

<?xml version="1.0" encoding="ISO-8859-15" ?>
<rss version="2.0">
    <channel>
        <title>test</title>
        <link>http://test.fr</link>
    </channel>
</rss>

Anyone can help me ?

Answer

hakre picture hakre · Oct 4, 2011

$link is an object which can not be converted to string (some objects can).

To see which object it is, use var_dump($link);. I assume it's a DOMElementDocs, see the link for all properties and methods it has to offer, e.g.

echo $link->tagName;

or

echo $link->textContent;