|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55459 Unable to differentiate between end of file or read error
Submitted: 2011-08-19 09:56 UTC Modified: 2011-08-19 10:46 UTC
From: scope at planetavent dot de Assigned:
Status: Wont fix Package: XML Reader
PHP Version: 5.3.7 OS: Ubuntu 10.04 LTS
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
7 + 6 = ?
Subscribe to this entry?

 [2011-08-19 09:56 UTC] scope at planetavent dot de
We were forced into using xmlreader the other day due to xml file sizes.

Usually we use DOM to check for well-formedness and schema validity.

It seems, that it is currently not possible to differentiate between a reading error (e.g. not well formed) and "end of file" using xmlreader.

Unfortunately it is not possible to call XMLReader::isValid() after an unsuccessful read when using a schema. Of course, if the document is not well-formed it can't be valid. But isValid() just calls onto xmlTextReaderIsValid() which seems to work only, if the node pointer was able to advance correctly (which is not the case for that kind of error). So this is not an option.

From ext/xmlreader/php_xmlreader.c:806
retval = xmlTextReaderRead(intern->ptr);
if (retval == -1) {
    php_error_docref(NULL TSRMLS_CC, E_WARNING, "An Error Occured while reading");
} else {


From ext/xmlreader/php_xmlreader.c:849
if (retval == -1) {
    php_error_docref(NULL TSRMLS_CC, E_WARNING, "An Error Occured while reading");
} else {

According to libxml, the result of xmlTextReaderRead() is
"1 if the node was read successfully, 0 if there is no more nodes to read, or -1 in case of error".

Therefor PHP should return true, int(0) or false to be able to check for reading errors.

I'm not quite sure if this can be considered a bug. To my mind it is, because it prevents me from using xmlreader in a proper way and as a result, renders it very difficult to work on huge xml files.

libxml is able to distinguish between both conditions, so should php.

Test script:

libxml_use_internal_errors( true );

$file = "input.xml";

$xr = new XMLReader();
$xr->open( $file );

while ( true )
    $success = $xr->read();
    if ( !$success )
        # end of file or error?
        var_dump( $success );
        echo "no success\n";
        echo "read success\n";

Expected result:
Ability to check for int(0) = no elements to read and false = error during read.

Actual result:
read success
read success
read success

Warning: XMLReader::read(): An Error Occured while reading in X:\xmlreader.php on line 12
no success


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-08-19 10:46 UTC]
-Status: Open +Status: Wont fix
 [2011-08-19 10:46 UTC]
If an error occurred libxml_get_errors() will be populated with details on the 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 12 22:01:29 2024 UTC