Closed arnisjuraga closed 6 years ago
Hi! No - XMLReader it is stream parser, and can not offset or seek position.
I did manage to "trick" it. I am readin ~1GB file, and do not want to parse all entries every time.
So I created simple modification so that parse()
method accepts parameter "lines", which then just loops through nodes without returning the result.
I am not 100% sure does it work correctly in any situation, but it works for me :)
public function parse($lines = 0 ) //<<-- parameter
{
for($i = 0; $i < $lines; $i++ ) { //<<-- just silly empty looping through "$lines" amount of nodes.
$this->read();
}
$this->read_lines = $i; //<<-- saving current position for later use
if (empty($this->callback)) {
throw new Exception("Empty parser callback.");
}
$continue = true;
while ($continue && $this->read()) {
$this->read_lines++; //<<--incrementing elements, and this is returned later from the Class instance
// and parsing can be continued from the last position if saved.
This is my base code for reading:
But every time the script is started, it will start file parsing from the beginning (obviously).
What I "save" current file position in some log text file, and next time I can "continue" reading from that position?
How this can be implemented?