QueryPath\DOMQuery::parseXMLFile PHP Method

parseXMLFile() private method

This attempts to autodetect the type of file, and then parse it.
private parseXMLFile ( string $filename, integer $flags = null, resource $context = null )
$filename string The file name to parse.
$flags integer The OR-combined flags accepted by the DOM parser. See the PHP documentation for DOM or for libxml.
$context resource The stream context for the file IO. If this is set, then an alternate parsing path is followed: The file is loaded by PHP's stream-aware IO facilities, read entirely into memory, and then handed off to {@link parseXMLString()}. On large files, this can have a performance impact.
    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;
    }