使用simplexml_load_file从XML提要中提取数据

时间:2012-04-25 22:24:36

标签: php xml api rss simplexml

我正在尝试使用simplexml_load_file从XML Feed中提取数据。这就是我现在所拥有的:

    <?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)。

1 个答案:

答案 0 :(得分: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;
    ?>