2016-04-25 88 views
0

如果我有這種格式的大型XML文件:PHP返回特定數量的節點?

<person> 
    <attribute>value</attribue> 
</person> 
<person> 
    <attribute>value</attribue> 
</person> 
<person> 
    <attribute>value</attribue> 
</person> 

有數百上千的<person>記錄....

我如何使用PHP,echo輸出到文件/回報'人'1-10,然後11-20等。

我發現了大量的例子(xpath等),返回所有節點或節點子集,但沒有返回特定範圍節點的示例。

我的具體文件有數十萬個<person>節點。我想以xml格式輸出<person> s 1-50,000,然後50,001-100,000等...

回答

0

原來相當簡單。我在網上找不到一個具體的例子,但我設法將幾個不同的例子拼湊成我需要的東西:

<?php 

$xml = simplexml_load_file('somexmlfile.xml'); 

$result = $xml->xpath('/enterprise/person[position()>=1 and position()<=10000]') 
; 

while(list(, $node) = each($result)) { 
    echo $node->asXML(); 
} 
?>