dkrnl / SimpleXMLReader

Wrapped XMLReader class, for simple SAX-reading of huge xml.
112 stars 43 forks source link

How to "resume" xml parsing from the last position, where it was left off? #11

Closed arnisjuraga closed 6 years ago

arnisjuraga commented 7 years ago

This is my base code for reading:

$reader = new exampleXmlReader;
$reader->open($file);
$reader->parse();
$reader->close();

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?

dkrnl commented 7 years ago

Hi! No - XMLReader it is stream parser, and can not offset or seek position.

arnisjuraga commented 7 years ago

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.