Can SimpleXML load only a partial of a XML?

后端 未结 2 1256
孤街浪徒
孤街浪徒 2021-01-13 13:52

Is there a way not to load the whole feed but only the first 10 tag?

$feed = \'rss file\';
$xml = simplexml_load_file(         


        
2条回答
  •  悲哀的现实
    2021-01-13 14:15

    No. A DOM parser (such as SimpleXML) can only load the entire document.

    But you can use XPath to filter the relevant parts:

    $xml   = simplexml_load_file($feed);
    $top10 = $xml->xpath('/rss/channel/item[position() <= 10]');
    
    foreach($top10 as $item) {
       // output $item;
    }
    

提交回复
热议问题