I\'m fetching xml files from a server and sometimes I\'m getting a non-valid xml files, because of this I\'m getting a warning:
Warning: DOMDocument::load()
Use set_error_handler.
set_error_handler("yourHandler", E_WARNING);
You have two choices. Either use the @
error control operator in your load()
call, e.g. @$dom->load()
, which is somewhat slow because it globally changes the value of display_errors to off, executes the function and sets it back to on.
The other option, which I personally prefer (I hate the @
operator, I can't stand to see it in my code) is to save the old value of libxml_use_internal_errors, enable it using libxml_use_internal_errors(TRUE)
, call the function, clear the errors buffer and restore the old value. Here's a snippet from my code that does that:
<?php
$previous_value = libxml_use_internal_errors(TRUE);
$doc->loadHTML((string)$e->response->getBody());
libxml_clear_errors();
libxml_use_internal_errors($previous_value);
I can't comment on answers yet, so I'll write it here:
nadav@shesek:~$ php -r '$dom=new DOMDocument; $dom->strictErrorChecking = FALSE ; $dom->loadHTML("<xy></zx>");' PHP Warning: DOMDocument::loadHTML(): Tag xy invalid in Entity, line: 1 in Command line code on line 1
Turn off strict error checking:
$dom = new DOMDocument();
$dom->strictErrorChecking = FALSE ;
$dom->load('/path/to/file.xml');
if (!$dom->validate()) {
// Invalid XML!
}