XML News from Wednesday, January 31, 2007

IBM's developerWorks has published Pull parsing XML in PHP, an introductory tutorial about PHP5's new XMLReader class:

PHP 5 introduced XMLReader, a new class for reading Extensible Markup Language (XML). Unlike SimpleXML or the Document Object Model (DOM), XMLReader operates in streaming mode. That is, it reads the document from start to finish. You can begin to work with the content at the beginning before you see the content at the end. This makes it very fast, very efficient, and very parsimonious with memory. The larger the documents you need to process, the more important this is.

Unlike the Simple API for XML (SAX), XMLReader is a pull parser rather than a push parser. This means that your program is in control. Rather than being told what the parser sees when the parser sees it, you tell the parser when to go fetch the next piece of the document. You request content rather than react to it. Another way of thinking about it: XMLReader is an implementation of the Iterator design pattern rather than the Observer design pattern.