PHP Function Reference

PHP xml_error_string() Function



The PHP xml_error_string() function returns the XML parser error string associated with the given error_code. The function returns a string with a textual description of the error error_code, or false if no description was found.

Syntax

xml_error_string(error_code)

Parameters

error_code Required. Specify an error code from xml_get_error_code() function.

Return Value

Returns a string with a textual description of the error error_code, or false if no description was found.

Example: Error (String do not enclosed in double quotes)

Lets assume that we have a file called demo.xml. This file contains following content. The content contains error (string which enclosed in double quotes).

<?xml version="1.0 encoding="UTF-8"?>
<userlist>
  <user>
    <username>John123</username>
    <name>John Smith</name>
    <phone>+1-8054098000</phone>
    <address>Brooklyn, New York, USA</address>
  </user>
</userlist>  

In the example below, the XML file is parsed. It displays the error while parsing the document.

<?php
//create an XML parser
$parser=xml_parser_create();

//opening xml file
$fp = fopen("demo.xml", "r");

while($data = fread($fp,4096)) {
  //parsing XML data
  if(!xml_parse($parser, $data, feof($fp))) {
    //displaying errors
    die(print "Error: " .
      //error string
      xml_error_string(xml_get_error_code($parser)) .
       
      "<br/>Error Code: " . 
      //error code
      xml_get_error_code($parser) .
       
      "<br/>Line: " .
      //line number where the error occurred
      xml_get_current_line_number($parser) .
       
      "<br/>Column: " .
      //column number where the error occurred
      xml_get_current_column_number($parser) .
       
      "<br/>Byte Index: " .
      //byte index where the error occurred
      xml_get_current_byte_index($parser) . "<br/>"
    );
  }
}

//free XML parser
xml_get_error_coder_free($parser);

fclose($fp);
?>

The output of the above code will be:

Error: String not closed expecting " or '
Error Code: 34
Line: 1
Column: 38
Byte Index: 37

Example: Error (Mismatched tag)

Lets assume that we have a file called example.xml. This file contains following content. The content contains error (mismatched tag).

<?xml version="1.0" encoding="UTF-8"?>
<userlist>
  <user>
    <username>John123</username>
    <name>John Smith</name>
    <phone>+1-8054098000</phone>
    <address>Brooklyn, New York, USA<address>
  </user>
</userlist>  

In the example below, the XML file is parsed. It displays the error while parsing the document.

<?php
//create an XML parser
$parser=xml_parser_create();

//opening xml file
$fp = fopen("example.xml", "r");

while($data = fread($fp,4096)) {
  //parsing XML data
  if(!xml_parse($parser, $data, feof($fp))) {
    //displaying errors
    die(print "Error: " .
      //error string
      xml_error_string(xml_get_error_code($parser)) .
       
      "<br/>Error Code: " . 
      //error code
      xml_get_error_code($parser) .
       
      "<br/>Line: " .
      //line number where the error occurred
      xml_get_current_line_number($parser) .
       
      "<br/>Column: " .
      //column number where the error occurred
      xml_get_current_column_number($parser) .
       
      "<br/>Byte Index: " .
      //byte index where the error occurred
      xml_get_current_byte_index($parser) . "<br/>"
    );
  }
}

//free XML parser
xml_get_error_coder_free($parser);

fclose($fp);
?>

The output of the above code will be:

Error: Mismatched tag
Error Code: 76
Line: 8
Column: 10
Byte Index: 220

❮ PHP XML Parser Reference