2

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?

2
  • where is the said xml feed example? so that we could see what you're trying to reach Commented Nov 26, 2014 at 11:15
  • @Ghost Now i provided the structure of my feed Commented Nov 26, 2014 at 11:52

1 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Previously I did it with DOMDocument. But is there no change to do it with simplexml_load_file?
@pbaldauf i don't think simplexml can handle comments, when you try to access it, it will return the parent node instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.