In PHP, libxml_get_errors() returns an array of all errors currently stored in the libxml error buffer. Each item is a LibXMLError object.
Use it after turning on libxml_use_internal_errors(true). It lets you loop through every warning and error to see the message, the line number, and how serious the problem is.
Syntax
Call libxml_get_errors() with the values you need. Start with this line.
PHP
$errors = libxml_get_errors();
Call
What it does
libxml_get_errors()
Returns an array of LibXMLError objects, or an empty array if there are none.
count(libxml_get_errors())
Returns how many errors are currently stored.
libxml_get_errors()[0]->message
Reads the message of the first error (only if the array is not empty).
Each LibXMLError object has these properties: level, code, column, message, file, and line. The level is one of LIBXML_ERR_WARNING, LIBXML_ERR_ERROR, or LIBXML_ERR_FATAL. Messages usually end with a newline, so use trim() before printing.
Library declarations
PHP
libxml_get_errors(): array
Parameters
Parameter
Description
—
This function takes no parameters.
Return Value
Return Value
Description
array
An array of LibXMLError objects. The array is empty when no errors were collected.
PHP Examples
Example 1 — Read the First Error
Parse broken XML quietly, then read the line number and message of the first error.
A well-formed document adds nothing to the buffer, so the count is 0.
A broken document adds one or more errors.
Calling libxml_get_errors() twice returns the same number of errors.
That proves reading the errors does not clear them.
Example 3 — Find the Line of the Problem
Use the line property to tell users exactly where the XML is broken.
PHP
<?php
libxml_use_internal_errors(true);
$xml = "<root>\n <item>One</item>\n <item>Two</root>";
$doc = new DOMDocument();
if (!$doc->loadXML($xml)) {
$errors = libxml_get_errors();
echo "Problem on line ", $errors[0]->line, PHP_EOL;
echo "Total errors found: ", count($errors) > 0 ? "at least 1" : "none", PHP_EOL;
}
libxml_clear_errors();
Output
Problem on line 3
Total errors found: at least 1
How It Works
The XML string has three lines, and the last item tag is not closed properly.
DOMDocument::loadXML() returns false for this broken document.
The first error reports line 3, where the mismatch was found.
libxml_clear_errors() cleans up the buffer at the end.
Frequently Asked Questions
It returns an array of LibXMLError objects for every error stored in the libxml buffer. If nothing went wrong, the array is empty.
You probably did not call libxml_use_internal_errors(true). Without it, libxml prints warnings straight to the output instead of storing them in the buffer.
level, code, column, message, file, and line. The level tells you if it is a warning, an error, or a fatal error.
libxml_get_errors() returns all collected errors as an array. libxml_get_last_error() returns only the most recent one, or false if there is none.
No. The errors stay in the buffer until you call libxml_clear_errors() or turn internal errors off.