Search code examples
phpxmlxmlreader

How does XmlReader::next position the cursor when it returns false


If I have some XML, for example:

<library>
    <books>
        <book>
            <title>book #1</title>
        </book>
        <book>
            <title>book #2</title>
        </book>
    </books>
    <authors>
         <author>
             <name>author #1</name>
         </author>
         <author>
             <name>author #2</name>
         </author>
    </authors>
</library>

And having manoeuvred my cursor to the first book element I loop using:

do {
    //...
} while($xml->next("book"));

When my cursor is on the last book element and the next("book") is called, what happens to the cursor?

  • Does it stay on the last book element and return false.
  • Does it move to the </library> and return false.
  • Does it do something else?

I cannot figure out how to get the current cursor position to figure this out for myself.


Solution

  • Having looked at the source code of that function: http://lxr.php.net/xref/PHP_5_4/ext/xmlreader/php_xmlreader.c#821

    It would appear that pointer moves to the end of the XML.