2

This is a beginners question (sorry for that); I am using simplexml_load_string() php function to parse an RSS feed, my code looks like this:

<?php

$url    = "http://www.zdnet.com/blog/rss.xml";
$xml    = file_get_contents($url);
$feed   = simplexml_load_string($xml);

foreach($feed->channel as $channel) {
    echo "Link : " . $channel->link . "<P>";
    echo "Image URL : " . $channel->image->url . "<P>";
}

foreach($feed->channel->item as $item) {
    echo "<HR><P>";
    echo "Link : " . $item->link . "<P>";
    echo "Title : " . $item->title . "<P>";
    echo "Description : " . $item->description . "<P>";
    echo "Date : " . $item->pubDate . "<P>";
}

?>

Which is working fine but the XML file has some tags that look like this:

<media:credit role="author">
<![CDATA[ James Kendrick ]]>
</media:credit>

<media:text type="html">
<![CDATA[
<figure class="alignRight"><a href="/i/story/70/00/034218/kindle-iphone-2.jpg" target="_blank">     
<img title="kindle-iphone-2" alt="kindle-iphone-2" src="http://cdn-static.zdnet.com/1.jpg"> 
height="237" width="200"></a><figcaption>(I
]]>

<![CDATA[
mage: James Kendrick/ ZDNet)</figcaption></figure> <p>Regular readers know I am a tablet guy
]]>
</media:text>

How can I parse these tags?

Thanks

2
  • Usually you wont parse CDATA sections at all. Just handle the as plain text Commented Oct 1, 2014 at 11:38
  • You may have a look at stackoverflow.com/questions/16547992/… Commented Oct 1, 2014 at 11:43

1 Answer 1

0

For accessing that nodes with namespaces you could use ->children() in that case:

$url = "http://www.zdnet.com/blog/rss.xml";
$feed = simplexml_load_file($url, null, LIBXML_NOCDATA);

foreach($feed->channel->item as $item) {
    echo "<HR><P>";
    echo "Link : " . $item->link . "<P>";
    echo "Title : " . $item->title . "<P>";
    echo "Description : " . $item->description . "<P>";
    echo "Date : " . $item->pubDate . "<P>";

    $media = $item->children('media', 'http://search.yahoo.com/mrss/'); // medias
    // then just loop the children
    // foreach($media as $m) {}
    $s = $item->children('s', 'http://www.zdnet.com/search'); // ss
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.