duanfen9090 2015-11-09 02:32 采纳率: 100%
浏览 48
已采纳

阅读RSS提要

I need to read an XML file, i watched some tutorials and tried different sollutions, but for some reason I can't figure out why it doenst work.

The XML file that I want to read: http://www.voetbalzone.nl/rss/rss.xml

This is the code that im using:

$xml= "http://www.voetbalzone.nl/rss/rss.xml"

for ($i = 0; $i < 10; $i++)
{
    $title = $xml->rss->channel->item[$i]->title;
}

The error I get: Premature end of data in tag

  • 写回答

1条回答 默认 最新

  • dqpciw9742 2015-11-09 03:57
    关注

    It works for me like this:

    <?php
    $xml = simplexml_load_file("http://www.voetbalzone.nl/rss/rss.xml");
    
    for ($i = 0; $i < 10; $i++)
    {
        $title = $xml->channel->item[$i]->title;
    }
    
    ?>
    

    Note that you are overwriting the variable $title each time, so that you will have the title of the 10. element in it after the loop finished [I assume that is not what you want?]

    To get all 'item'-Elements inside 'channel' as an Array to iterate through you can use xpath like this:

    <?php
    $xml = simplexml_load_file("http://www.voetbalzone.nl/rss/rss.xml");
    
    $item_array = $xml->xpath("//rss/channel/item");
    
    foreach($item_array as $item) {
        echo $item->title . "
    ";
    }
    ?>
    

    I would suggest to read about php's SimpleXML here: http://php.net/manual/en/book.simplexml.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部