You are here

private function QueryPath::parseXMLFile in QueryPath 6

Same name and namespace in other branches
  1. 7.3 QueryPath/QueryPath.php \QueryPath::parseXMLFile()
  2. 7.2 QueryPath/QueryPath.php \QueryPath::parseXMLFile()
1 call to QueryPath::parseXMLFile()
QueryPath::__construct in QueryPath/QueryPath.php

File

QueryPath/QueryPath.php, line 1942

Class

QueryPath

Code

private function parseXMLFile($filename, $flags = NULL, $context = NULL) {
  if (!empty($context)) {
    try {
      set_error_handler(array(
        'QueryPathParseException',
        '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 QueryPathParseException(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,
  );
  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(
      'QueryPathParseException',
      'initializeFromError',
    ), $this->errTypes);
    if ($useParser == 'xml') {
      $r = $document
        ->load($filename, $flags);
    }
    elseif (isset($htmlExtensions[$ext]) || $useParser == 'html') {
      $r = $document
        ->loadHTMLFile($filename);
    }
    else {
      $r = $document
        ->load($filename, $flags);
    }
  } catch (Exception $e) {
    restore_error_handler();
    throw $e;
  }
  restore_error_handler();
  return $document;
}