PHP Function Reference

PHP libxml_clear_errors() Function



The PHP libxml_clear_errors() function clears the libxml error buffer.

Syntax

libxml_clear_errors()

Parameters

No parameter is required.

Return Value

No value is returned.

Example:

The example below shows how to build a simple libxml error handler.

<?php
libxml_use_internal_errors(true);

//contains mismatched tags
$xmlstr = <<<XML
<mail> 
  <To>John Smith</too>
  <From>Marry G.</From>
  <Subject>Happy Birthday</Subject>
  <body>Happy birthday. Live your life with smiles.</body>
</mail> 
XML;

$doc = simplexml_load_string($xmlstr);
$xml = explode("\n", $xmlstr);

if ($doc === false) {
  $errors = libxml_get_errors();

  //displaying an error occurred
  foreach ($errors as $error) {
    echo display_xml_error($error, $xml);
  }

  //CLEARING THE libxml ERROR BUFFER
  libxml_clear_errors();
}

//function to display error message
function display_xml_error($error, $xml) {
  $message = "";
  switch ($error->level) {
    case LIBXML_ERR_WARNING:
      $message .= "Warning $error->code: ";
      break;
    case LIBXML_ERR_ERROR:
      $message .= "Error $error->code: ";
      break;
    case LIBXML_ERR_FATAL:
      $message .= "Fatal Error $error->code: ";
      break;
  }

  $message .= trim($error->message) .
    "\n Line: $error->line" .
    "\n Column: $error->column";

  if ($error->file) {
    $message .= "\n File: $error->file";
  }

    return $message;
}
?>

The output of the above code will be:

Fatal Error 76: Opening and ending tag mismatch: To line 2 and too
 Line: 2
 Column: 23

❮ PHP Libxml Reference