SimpleXML PHP5: last items in large set
When I use SimpleXML it puts newer nodes at the end of the file.
Assuming my file is large (think massive and growing) what is the best way to get the last n nodes.
For example:
<topic title="epic 129109 page post race">
<post date="ages-ago">First!</post>
...
<post date="seconds-ago">Thank you for that</post>
<post date="just-now">No, thank you.</post>
</topic>
I don't want to kill my poor server by asking it to foreach through a growing set if I can skip to the last n nodes.
The question is how do I do this?
Re: SimpleXML PHP5: last items in large set
Your first port of call should be the XMLReader. This is better to suited to an XML dataset containing a large quantity of data as it does not load the entire data set into memory.
However, this does not solve the problem of moving the last-nth element. You will have to call the "next()" function until you reach your desired location and you will need to record the number of occurrences of the element in an attribute of its parent element. I recommend that if you have a very large data set, you consider using a database or if the data must stay in XML format an XML database; I've used Berkeley DB XML and it is very, very fast.