Parsing a FeedBurner feed with SimpleXML

Parsing a FeedBurner feed with SimpleXML requires only a single gotcha: the actual parsing starts from the root element, not from the whole object created with the simplexml_load_file() function. For example, the following code returns nothing:

$feed = simplexml_load_file('http://feeds.feedburner.com/blogspot/onwebdev/');

foreach($feed->item as $item) {

    //... nothing here

}

Instead, the following code works as expected:

 $feed = simplexml_load_file('http://feeds.feedburner.com/blogspot/onwebdev/');
    
    foreach($feed->channel->item as $item) {
    
        $title = $item->title;
        $raw_author = $item->author;
        $author = str_replace('gabriele.romanato@gmail.com', '', $raw_author);
        $author = str_replace('(', '', $author);
        $author = str_replace(')', '', $author);
        $links = $item->children('http://rssnamespace.org/feedburner/ext/1.0');
        $link = $links->origLink;
        $raw_date = $item->pubDate;
        $pubdate = str_replace('+0000', '', $raw_date);
        
        echo '<li><a href="' . $link . '">' . $title . '</a>' . "\n" . '<div class="author">' . $author . '</div>' . "\n" .
        '<div class="pubdate">' . $pubdate . '</div>' . "</li>\n";
    
    
    }

You can notice that now the parsing starts from the channel element. You can see the final result here.

Leave a Reply

Note: Only a member of this blog may post a comment.