2

我正在尝试使用 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

1 回答 1

4

您应该遵循 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;
    ?>
于 2012-04-25T22:40:57.767 回答