SimpleXML 能否仅加载部分 XML? [英] Can SimpleXML load only a partial of a XML?
本文介绍了SimpleXML 能否仅加载部分 XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法不加载整个提要而只加载前 10 个 <item></item>
标签?
Is there a way not to load the whole feed but only the first 10 <item></item>
tag?
$feed = 'rss file';
$xml = simplexml_load_file($feed); //not the entire feed though
推荐答案
没有.DOM 解析器(例如 SimpleXML)只能加载整个文档.
No. A DOM parser (such as SimpleXML) can only load the entire document.
但是你可以使用XPath来过滤相关部分:
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;
}
这篇关于SimpleXML 能否仅加载部分 XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文