In need to speed up this code that filters the data of XML using XMLReader I am using XMLReader and avoided SimpleXML because I must handle a huge file and for memory issues, SimpleXML is not the ideal solution. However, even I coded the below script in SimpleXML, it gives me the result really much faster.

Because speed and memory is a must for this project, is there any way to speed up this code? What it does, it filters the books and show those that contains the word jQuery.

error_reporting(~0);

$xml = &lt;&lt;&lt;EOD
&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
&lt;library&gt;
    &lt;book isbn="781"&gt;
        &lt;name&gt;SCJP 1.5&lt;/name&gt;
        &lt;info&gt;Sun Certified Java Programmer book&lt;/info&gt;
    &lt;/book&gt;
    &lt;book isbn="194"&gt;
        &lt;name&gt;jQuery is Awesome!&lt;/name&gt;
        &lt;info&gt;jQuery Reference Book&lt;/info&gt;
    &lt;/book&gt; 
    &lt;book isbn="199"&gt;
        &lt;name&gt;jQuery 101&lt;/name&gt;
        &lt;info&gt;All you need to know about jQuery&lt;/info&gt;
    &lt;/book&gt; 
&lt;/library&gt;
EOD;

$file = 'data://text/plain;base64,'.base64_encode($xml);

class XMLBookIterator implements iterator
{
    private $file;
    private $reader;
    private $state;
    private $key = 0;
    private $book;
    private $valid;
    public function __construct($file)
    {
        $this-&gt;file = $file;
    }
    public function current()
    {
        return $this-&gt;book;
    }
    public function key()
    {
        return $this-&gt;key;
    }
    public function next()
    {
        $reader = $this-&gt;reader;
        while ($next = $reader-&gt;read())
        {
            switch ($reader-&gt;nodeType)
            {
                case (XMLREADER::ELEMENT):
                    $case = $reader-&gt;localName.'|'.$this-&gt;state;
                    switch($case)
                    {
                        case 'library|0':
                            break;
                        case 'book|1':
                            $this-&gt;book = new stdClass;
                            $this-&gt;book-&gt;isbn = $reader-&gt;getAttribute('isbn');                
                            break;
                        case 'name|2':
                            $this-&gt;book-&gt;name = $reader-&gt;readInnerXML();
                            break;
                        case 'info|3':
                            $this-&gt;book-&gt;info = $reader-&gt;readInnerXML();
                            $this-&gt;state=0;
                            break;
                        default:
                            throw new Exception(sprintf('Invalid State: %s.', $case));
                    }
                    $this-&gt;state++;
                    if ($this-&gt;state === 1) break 2;
            }
        }
        $this-&gt;valid = $next;
        $this-&gt;key++;
    }
    public function rewind()
    {
        if ($this-&gt;reader) $this-&gt;reader-&gt;close();
        $this-&gt;reader = new XMLReader();
        $this-&gt;reader-&gt;open($this-&gt;file);
        $this-&gt;state = 0;
        $this-&gt;next(); // move to first element or invalidate
        if ($this-&gt;valid) $this-&gt;next(); // get first book
        $this-&gt;key = 0;
    }
    public function valid()
    {
        return $this-&gt;valid;
    }
}

class BookFilterIterator extends FilterIterator
{
    private $term;
    public function __construct($iterator, $term)
    {
        parent::__construct($iterator);
        $this-&gt;term = $term;
    }
    public function accept() {
        $book = parent::current();
        $name = $book-&gt;name;
        return false !== stripos($name, $this-&gt;term);
    }
}

// filtered books:

$books = new XMLBookIterator($file);

$filtered = new BookFilterIterator($books, 'jQuery');

foreach($filtered as $key =&gt; $book)
{
    echo 'book (', $key, '):', "\n";
    print_r($book);
}
Your code seems quite tidy and easy to understand, congratulations. But maybe you can check for built in functionality. XPath can search items for you:

http://www.php.net/manual/en/simplexmlelement.xpath.php
http://www.w3schools.com/xpath/xpath_syntax.asp