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条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统