This method throws E_NOTICE errors for any validation errors. If you'd rather store validation errors in an array for gracious handling, you can use the code below.
<?php
/**
* Hold thrown errors statically
*/
function staticerror($errno, $errstr, $errfile, $errline, $errcontext, $ret = false)
{
static $errs = array();
if ($ret === true) {
return $errs;
}
$tag = 'DOMDocument::validate(): ';
$errs[] = str_replace($tag, '', $errstr);
}
// Load a document
$dom = new DOMDocument;
$dom->load('somefile');
// Set up error handling
set_error_handler('staticerror');
$old = ini_set('html_errors', false);
// Validate
$doc->validate();
// Restore error handling
ini_set('html_errors', $old);
restore_error_handler();
// Get errors
$errs = staticerror(null, null, null, null, null, true);
print_r($errs);
?>