libxml_get_errors()
Today, we will discuss the libxml_get_errors() function in PHP. This function is used to retrieve any errors that have been generated by the libxml functions.
The libxml_get_errors() function in PHP retrieves the list of errors and warnings generated by libxml functions — the C library that powers PHP's DOMDocument, SimpleXML, and XMLReader extensions. It is typically called after parsing or validating an XML document so you can inspect exactly what went wrong instead of relying on PHP warnings.
This page covers the function signature, the structure of the error objects it returns, a self-contained runnable example, how to read error severity levels, and how it relates to the other libxml error functions.
Syntax
libxml_get_errors(): arrayIt takes no arguments and returns an array of LibXMLError objects. If no errors are in the buffer, it returns an empty array ([]).
What libxml_get_errors() Returns
Each element in the returned array is a LibXMLError object with these public properties:
| Property | Type | Description |
|---|---|---|
level | int | Severity: LIBXML_ERR_NONE (0), LIBXML_ERR_WARNING (1), LIBXML_ERR_ERROR (2), or LIBXML_ERR_FATAL (3). |
code | int | The internal libxml error code. |
message | string | The human-readable error message (note the trailing \n). |
file | string | The filename, or an empty string when parsing a string in memory. |
line | int | The line number where the error occurred. |
column | int | The column number where the error occurred. |
How to Use libxml_get_errors()
There is one rule you must follow: enable internal error handling first. By default libxml writes parse errors straight to PHP's output as warnings, and the error buffer stays empty. Calling libxml_use_internal_errors(true) redirects those errors into an internal buffer that libxml_get_errors() can read.
The typical workflow is:
- Call
libxml_use_internal_errors(true)before parsing. - Parse or validate the XML (with
DOMDocument,SimpleXML, etc.). - Call
libxml_get_errors()to read the collected errors. - Call
libxml_clear_errors()to empty the buffer for the next operation.
A self-contained, runnable example
This example loads malformed XML from a string (no external files needed) and prints each error with its severity, line, and column:
<?php
// 1. Capture errors in the internal buffer instead of emitting warnings
libxml_use_internal_errors(true);
// Intentionally broken XML: <author> is never closed
$xml = <<<XML
<book>
<title>PHP Basics</title>
<author>Jane Doe
</book>
XML;
// 2. Parse it
$doc = new DOMDocument();
$doc->loadXML($xml);
// 3. Read the errors
$errors = libxml_get_errors();
// Map numeric levels to readable labels
$levels = [
LIBXML_ERR_WARNING => 'Warning',
LIBXML_ERR_ERROR => 'Error',
LIBXML_ERR_FATAL => 'Fatal',
];
foreach ($errors as $error) {
printf(
"[%s] line %d, col %d: %s",
$levels[$error->level] ?? 'Unknown',
$error->line,
$error->column,
$error->message // already ends with a newline
);
}
// 4. Clear the buffer so it doesn't leak into later parsing
libxml_clear_errors();
?>Output:
[Fatal] line 4, col 8: Opening and ending tag mismatch: author line 3 and book
[Fatal] line 4, col 8: Premature end of data in tag book line 1Because the <author> tag is left open, libxml reports two fatal errors, each pointing at the exact line and column where it detected a problem. Reading level, line, and column — rather than just message — is what makes libxml_get_errors() so useful for building precise validation feedback.
Validating against a schema
The same pattern works for schema validation. After schemaValidate() / schemaValidateSource() returns false, the validation errors are waiting in the buffer:
<?php
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->load('example.xml');
// schemaValidateSource() validates against an in-memory XSD string
$xsd = file_get_contents('example.xsd');
if ($doc->schemaValidateSource($xsd)) {
echo "The XML document is valid.\n";
} else {
echo "The XML document is not valid:\n";
foreach (libxml_get_errors() as $error) {
echo trim($error->message) . "\n";
}
}
libxml_clear_errors();
?>Tip: If you only need the single most recent problem, use
libxml_get_last_error()instead — it returns oneLibXMLErrorobject (orfalse) rather than the whole array.
Common Gotchas
- Empty result? You almost certainly forgot
libxml_use_internal_errors(true). Without it the buffer is never populated. - Stale errors. The buffer is shared across all libxml operations in the request. Always call
libxml_clear_errors()after you finish handling a batch, or earlier errors will resurface in a laterlibxml_get_errors()call. - Warnings vs. fatals. A non-empty array doesn't necessarily mean the document is unusable — filter on
$error->level >= LIBXML_ERR_ERRORif you want to ignore warnings.
Related Functions
libxml_use_internal_errors()— turn the error buffer on or off.libxml_get_last_error()— fetch only the most recent error.libxml_clear_errors()— empty the error buffer.- PHP libxml overview — the broader extension these functions belong to.
- PHP XML DOM — parsing documents with
DOMDocument.
Conclusion
The libxml_get_errors() function is a crucial tool for debugging XML operations in PHP. By enabling internal error handling with libxml_use_internal_errors(true), parsing your document, and then inspecting each LibXMLError object's level, line, column, and message, you can build precise, user-friendly XML validation instead of swallowing raw PHP warnings. Remember to clear the buffer with libxml_clear_errors() between operations.