Pull Parsing

Pull parsing regards an XML document as a series of objects that are read sequentially using the Iterator design pattern: the application explicitly requests, or "pulls", the next parsing event from the parser, rather than the parser calling back into application code. This makes pull parsing compatible with recursive-descent parsers, which mirror the structure of the XML being read.

Because control flow stays with the application code, in an ordinary loop, pull-parsing code is generally easier to understand than SAX parsing code, which is driven by callbacks. StAX in Java and XMLReader in PHP are examples of pull parsers.

PatternIterator design pattern; the application pulls the next event
ContrastOpposite of push-based SAX, where the parser calls back into the application
CompatibilityMaps naturally onto recursive-descent parsers that mirror the XML structure
ExamplesStAX (Java), XMLReader (PHP), XmlReader (.NET)

<?php
$reader = new XMLReader();
$reader->open('books.xml');

while ($reader->read()) {
    if ($reader->nodeType == XMLReader::ELEMENT && $reader->localName == 'book') {
        echo "Book id: " . $reader->getAttribute('id') . "\n";
    }
}
$reader->close();
?>