dongzongzi0379 2013-06-19 22:22
浏览 31
已采纳

PHP RSS提要阅读器效率

I'm reading data from an XML feed as follows:

$data=file_get_contents("mydata.rss");

$data=simplexml_load_string($data);

foreach($data->channel->item as $item){ 

     $articles[] = array(
                    'description' => (string)$item->description,
                    'link' => (string)$item->link,
                    'pubDate' => (string)$item->pubDate,);

} 

The issue is that the feed is very long with maybe 100 items. I only want read the first 10. I can work around this by manually setting a counter and then using an if statement within the foreach loop but I don't think that is the best approach as the entire feed is still be read and therefore unecessary overhead is added.

what's the most efficient way of achieving this without reading the entire feed?

Thanks in advance...

  • 写回答

1条回答 默认 最新

  • doubushi0031 2013-06-19 22:56
    关注

    Using SimpleXML, as you say, you load all the file in memory and then it's parsed. Then you iterate over the loaded elements in memory.

    Using a SAX-like parser like "XML Parser", will allow you don't read the full file. I don't know how exactly is implemented, but the aproach in SAX is fire an event every time a new element is detected. Then, you can start reading the RSS and stop parsing when the 10th element of type "item" is closed.

    This aproach has smaller memory footprint and is faster. In the other hand, it's not as easy iterate over the elements of the XML.

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

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)