开发者

simple xml parsing

firstly im a bit of a php noob, thought i had simple xml down but apparently not :)

i'm simply trying to parse a news feed and echo it out, but i'm getting nothing, and no error message which makes it harder. I'm probably making a stupid mistake...

heres the code i've written:

<?php

$rss = simplexml_load_file('http://feeds.bbci.co.uk/news/world/rss.xml');

foreach($rss->channel->item as $item) {

    $title = $item['title'];

    echo $title;
    echo '<br /><hr />';
}

?>

heres an example of the xml layout:

<rss xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">  
  <channel> 
    <title>BBC News - World</title>  
    <link>http://www.bbc.co.uk/go/rss/int/news/-/news/world/</link>  
    <description>The latest stories from the World section of the BBC News web site.</description> 开发者_JAVA技巧 
    <language>en-gb</language>  
    <lastBuildDate>Mon, 21 Mar 2011 14:31:31 GMT</lastBuildDate>  
    <copyright>Copyright: (C) British Broadcasting Corporation, see http://news.bbc.co.uk/2/hi/help/rss/4498287.stm for terms and conditions of reuse.</copyright>  
    <ttl>15</ttl>  
    <atom:link href="http://feeds.bbci.co.uk/news/world/rss.xml" rel="self" type="application/rss+xml"/>  
    <item> 
      <title>Gaddafi 'not targeted' by strikes</title>  
      <description>Coalition forces carrying out operations against Libyan government forces say Colonel Gaddafi himself is not a target, despite a strike on his compound.</description>  
      <link>http://www.bbc.co.uk/go/rss/int/news/-/news/world-africa-12802939</link>  
      <guid isPermaLink="false">http://www.bbc.co.uk/news/world-africa-12802939</guid>  
      <pubDate>Mon, 21 Mar 2011 14:39:21 GMT</pubDate>  
      <media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/51766000/jpg/_51766446_011582753-1.jpg"/>  
      <media:thumbnail width="144" height="81" url="http://news.bbcimg.co.uk/media/images/51766000/jpg/_51766444_011582753-1.jpg"/> 
    </item>
  </channel> 
</rss> 

can anybody see any glaring misktakes with my code? ive used it before and its worked fine :(

thanks for your time,

alsweet


Try this:

foreach($rss->channel->item as $item) {

    $title = (string)$item->title[0];

    echo $title;

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜