pbaldauf
pbaldauf

Reputation: 1681

PHP SimpleXML read comment with xPath

I'm loading a XML Feed, which works. But it seems that I am missing something in my xpath query. Actually I want to read the comment of the title node but it doesn't seem to work.

$xml = simplexml_load_file( $feed_url );
$comment = $xml->xpath('//channel/item[1]/title//comment()');

The feed has the following structure

<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
    <title>My main feed</title>
    <description>feed description</description>
    <link>http://www.example.com</link>
    <language>en_US</language>
    <lastBuildDate>Wed, 26 Nov 2014 11:44:13 UTC</lastBuildDate>

    <item>
        <title><!-- Special Image  --></title>
        <link></link>
        <guid>http://www.example.com/page/345</guid>
        <media:category>horizontal</media:category>
        <media:thumbnails>
            <media:thumbnail url="www.example.com/test.jpg" type="image/jpeg" height="324" width="545" />
        </media:thumbnails>
    </item>
    <item>
        <title>Here's a normal title</title>
        <description><![CDATA[Description Text]]></description>
        <link></link>
        <guid>http://www.example.com/page/123</guid>
        <media:category>horizontal</media:category>
    </item>
</channel>
</rss>

Does anyone have a clue how I could read the comment?

Upvotes: 2

Views: 872

Answers (1)

Kevin
Kevin

Reputation: 41885

Alternatively, you could use DOMDocument to access those comments. Example:

$dom = new DOMDocument();
$dom->load($feed_url);
$xpath = new DOMXpath($dom);
$comment = $xpath->evaluate('string(//channel/item[1]/title/comment())');
echo $comment;

Upvotes: 2

Related Questions