dongxie559554 2014-03-12 11:41
浏览 60
已采纳

PHP - 只从XML获取某些数据,而不是整个xml文件?

I need to get online users data from xml file, but simplexml_load_file seems slow. File is big because online users are a lot. I am trying to paginate them 20 per page I have...

<?php $xml = simplexml_load_file('http://domain.tld/data.xml'); ?>

Then

<?php echo $xml->who_is_online[1]->thumbnail_image; ?>
<?php echo $xml->who_is_online[1]->display_name; ?>
<?php echo $xml->who_is_online[1]->display_age; ?>

But there should be about 20 of these per page.

<?php echo $xml->who_is_online[2]->thumbnail_image; ?>
<?php echo $xml->who_is_online[2]->display_name; ?>
<?php echo $xml->who_is_online[2]->display_age; ?>
<?php echo $xml->who_is_online[3]->thumbnail_image; ?>
<?php echo $xml->who_is_online[3]->display_name; ?>
<?php echo $xml->who_is_online[3]->display_age; ?>

etc. (to 20)

What is the better way to get only this certain data from XML without reading the whole file on each page load? I mean get data for user 1 to user 20 on 1st page, then on 2nd page get data for user 21 to 40, etc. On 2nd page I have the same code but getting data for [21], [22], [23], etc.

  • 写回答

2条回答 默认 最新

  • dongyan5239 2014-03-12 12:00
    关注

    SimpleXML can't partially read a document. Even with an XPath filter, the whole document is still parsed behind the scenes.

    A possible solution is use a cronjob or scheduled task to regularly call a PHP script which downloads the XML, parses it and inserts the data into an SQL database (don't forget to delete the old data first).

    When a visitor requests a page, just paginate the data using SQL queries on the database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里