Skip to content

xml_error_string()

The xml_error_string() function is a PHP built-in function that retrieves a string description of an XML parser error. It belongs to the legacy XML Parser extension. When parsing XML files using SimpleXML or other modern libraries, errors are typically handled using libxml_get_errors() instead, as the legacy xml extension is deprecated in modern PHP.

The xml_error_string() function is useful when you need to retrieve a user-friendly error message for legacy XML parsing workflows, though modern applications should prefer libxml_get_errors() for SimpleXML.

Syntax

The syntax of the xml_error_string() function is as follows:

syntax of the xml_error_string() function

php
xml_error_string($code)

Where $code is the error code returned by the XML parser.

Usage Examples

Let's take a look at some practical examples of using xml_error_string() in PHP.

Example 1: Retrieving an XML Parser Error String (Legacy XML Parser)

The xml_error_string() function works with the legacy XML Parser extension. You can use it to parse XML and retrieve the error string, like this:

parse the XML file and retrieve the error string using xml_error_string() in PHP

php
$parser = xml_parser_create();
$xml_data = "<invalid xml>";
xml_parse($parser, $xml_data);

$error_code = xml_get_error_code($parser);
if ($error_code !== XML_ERROR_NONE) {
  $error_string = xml_error_string($error_code);
  echo "Error: $error_string";
}

xml_parser_free($parser);

This code creates an XML parser, attempts to parse invalid XML, and checks for errors. If an error occurred, it retrieves the error code using xml_get_error_code(), and then retrieves the error string using xml_error_string(). Finally, it prints the error string.

If you are using the SimpleXML library, xml_error_string() is not applicable. Instead, use libxml_use_internal_errors(true) and libxml_get_errors() to handle parsing failures:

Displaying an XML Parser Error Message

php
libxml_use_internal_errors(true);

if (isset($_FILES["xml_file"])) {
  $xml = simplexml_load_file($_FILES["xml_file"]["tmp_name"]);
  if ($xml === false) {
    $errors = libxml_get_errors();
    foreach ($errors as $error) {
      echo "Error: " . $error->message;
    }
  } else {
    // process the XML file
  }
}

This code enables internal error handling for SimpleXML, checks if an XML file was uploaded using the $_FILES array, and attempts to load the file. If an error occurs during parsing, it retrieves the error details using libxml_get_errors() and displays the error message to the user. If no errors occur, the code can process the XML file as needed.

Conclusion

In this article, we've discussed PHP's xml_error_string() function and how it can be used to retrieve a string description of an XML parser error in legacy XML parsing workflows. We've explained what the function does, its syntax, and provided examples of how it can be used. For modern PHP applications using SimpleXML, we recommend using libxml_use_internal_errors(true) and libxml_get_errors() to handle parsing failures, ensuring your web applications are more robust and user-friendly.

Practice

What does the libxml_get_errors() function in PHP do?

Dual-run preview — compare with live Symfony routes.