First of all, make sure that your XML file is indeed UTF-8 encoded. If not specify the encoding as the second parameter to XMLReader::open()
.
If the encoding error is due a real malformed byte sequence in an UTF-8 document and if you're using PHP > 5.2.0 you could pass LIBXML_NOERROR
and/or (depending on the error level) LIBXML_NOWARNING
as a bitmask to the third parameter of XMLReader::open()
:
$xml = new XMLReader();
$xml->open('myxml.xml', null, LIBXML_NOERROR | LIBXML_NOWARNING);
If your're using PHP > 5.1.0 you can tweak the libXML
error-handling.
// enable user error handling
libxml_use_internal_errors(true);
/* ... do your XML processing ... */
$errors = libxml_get_errors();
foreach ($errors as $error) {
// handle errors here
}
libxml_clear_errors();
I actually don't know if the preceding two work-arounds actually allow XMLReader
to continue reading in case of an error or if they only suppress the error output. But it's worth a try.
Responding to comment:
libXML
defines XML_PARSE_RECOVER
(1) but ext/libxml does not expose this constant as a PHP constant. Perhaps it's possible to pass the integer value 1
to the $options
parameter.
$xml = new XMLReader();
$xml->open('myxml.xml', null, LIBXML_NOERROR | LIBXML_NOWARNING | 1);