dongshi4078 2009-12-16 20:43
浏览 26
已采纳

PHP - 消除XML结果集的结果

I think this might be more accurately called "paging"... not sure. I am getting a chunk of XML (lets say 100 nodes) I then want to display only a set number of them at a time on a page. How do you do this?

For reference, in .NET it would be something like this:

    // Get some results. These are an XPathExpression
    XPathNodeIterator iterate = nav.Select(results);
    int index = 0;
    // Iterate over them deleting excess results
    foreach (XPathNavigator node in iterate) {
        if ((index < beginIndex) || (index > finishIndex)) {
            node.DeleteSelf();
        }
        index++;
    }
    // Set iterate to be this new set of results
    iterate = nav.Select(results);
    // Write out my new result set
    foreach (XPathNavigator node in iterate) {
        Response.Write(node.OuterXml);
    }

and that would grab a subset of XML from an XML document, run through it deleting all nodes greater or less than my beginIndex and my finishIndex (so I would grab, say the first 20 results by setting beginIndex = 0 and finishIndex = 19 and next pass through I could grab nodes 20-29 and so on).

In a nutshell, in PHP, how do you delete the unwanted nodes like that bit that says node.DeleteSelf? Everything else I can do... just not sure about that delete bit.

  • 写回答

2条回答 默认 最新

  • du4010 2009-12-16 21:10
    关注

    PHP's SimpleXMLElement documentation: http://php.net/manual/en/simplexmlelement.xpath.php

    Shows that SimpleXmlElement::xpath returns an array of SimpleXMLElement objects, so you could do something like:

    $result = $xml->xpath('string');
    for ($i = $startIndex; $i < $startIndex + $pageSize; $i++){
        // also make sure that $result[$i] exists
        // Then handle printing $result[$i]
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题