2012-04-25 80 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