private function parseXMLFile($filename, $flags = null, $context = null)
{
// If a context is specified, we basically have to do the reading in
// two steps:
if (!empty($context)) {
try {
set_error_handler(array('\\QueryPath\\ParseException', 'initializeFromError'), $this->errTypes);
$contents = file_get_contents($filename, false, $context);
} catch (Exception $e) {
restore_error_handler();
throw $e;
}
restore_error_handler();
if ($contents == false) {
throw new \QueryPath\ParseException(sprintf('Contents of the file %s could not be retrieved.', $filename));
}
return $this->parseXMLString($contents, $flags);
}
$document = new \DOMDocument();
$lastDot = strrpos($filename, '.');
$htmlExtensions = array('.html' => 1, '.htm' => 1);
// Allow users to override parser settings.
if (empty($this->options['use_parser'])) {
$useParser = '';
} else {
$useParser = strtolower($this->options['use_parser']);
}
$ext = $lastDot !== false ? strtolower(substr($filename, $lastDot)) : '';
try {
set_error_handler(array('\\QueryPath\\ParseException', 'initializeFromError'), $this->errTypes);
// If the parser is explicitly set to XML, use that parser.
if ($useParser == 'xml') {
$r = $document->load($filename, $flags);
} elseif (isset($htmlExtensions[$ext]) || $useParser == 'html') {
// Try parsing it as HTML.
$r = $document->loadHTMLFile($filename);
} else {
$r = $document->load($filename, $flags);
}
} catch (Exception $e) {
restore_error_handler();
throw $e;
}
restore_error_handler();
return $document;
}