PHP libXMLError class
The PHP libXMLError class contains various information about errors thrown by libxml.
Class synopsis
class libXMLError { //Properties public int $level; public int $code; public int $column; public string $message; public string $file; public int $line; }
Properties
level |
The severity of the error (one of the following constants: LIBXML_ERR_WARNING, LIBXML_ERR_ERROR or LIBXML_ERR_FATAL) |
code |
The error's code. |
column |
The column where the error occurred. |
message |
The error message, if any. |
file |
The filename, or empty if the XML was loaded from a string. |
line |
The line where the error occurred. |
Example:
The example below shows the usage of libXMLError class.
<?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_get_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