2012-04-25 83 views
2

我试图使用simplexml_load_file从XML提要提取数据。这是我现在有:使用simplexml_load_file从XML提要提取数据

<?php 
     $anobii = simplexml_load_file('http://www.anobii.com/rss_shelf?s=01fe251a6c442bbf8a'); 
     foreach ($anobii->entry as $anobiiinfo): 
      $title=$anobiiinfo->rss->channel->item->title; 
      $desc=$anobiiinfo->rss->channel->item->description;  
      echo "<span> ",$title,"</span><br><span> ",$desc,"</span>"; 
     endforeach; 
    ?> 

的问题是,我不知道正确的分隔符告诉脚本它需要提取的部分(rss->channel->item->title)。

回答

4

Yous应遵循xml树结构来获取单个项目。

<?php 
     $feedUrl = 'http://www.anobii.com/rss_shelf?s=01fe251a6c442bbf8a'; 
     $rawFeed = file_get_contents($feedUrl); 
     $anobii = new SimpleXmlElement($rawFeed); 

     foreach ($anobii->channel->item as $anobiiinfo): 
      $title=$anobiiinfo->title; 
      $desc=$anobiiinfo->description;  
      echo "<span> ",$title,"</span> <br/> <span> ",$desc,"</span>"; 
     endforeach; 
    ?> 
+0

谢谢!! :D 它的工作原理! 最新的问题:你使用这种方法(file_get_contents)还是内置函数fetch_feed(基于SimplePie)更好/更快? http://codex.wordpress.org/Function_Reference/fetch_feed 再次感谢! :) – MultiformeIngegno 2012-04-25 22:48:30

+0

我宁愿使用fetch_feed,这应该比使用rs URL获取提要更好。 – Jayaram 2012-04-25 22:56:55