PHP | xml_error_string() Function
Last Updated :
07 Aug, 2021
Pre-requisite: XML Basics
The xml_error_string() function is an inbuilt function in PHP which returns the XML parser error description for generated error code.
Syntax:
string xml_error_string( int $error_code)
Parameters: This function accepts single parameter $error_code which is required. It specifies an XML parser error code which is generated from the xml_get_error_code() function.
Return Values: This function returns the XML parser error description for specified error code generated from xml_get_error_code() on success and False on failure.
Note: This function is available for PHP 4.0.0 and newer version.
Below programs illustrate the xml_error_string() function in PHP:
gfg.xml file:
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< user >
< username > user123 </ username >
< name > firstname lastname </ name >
< phone > +91-9876543210 </ phone >
< detail > I am John Doe. Live in Kolkata, India. </ detail >
</ users >
|
Program 1:
PHP
<?php
$xml_file = 'gfg.xml' ;
$xml_parser = xml_parser_create();
$file_pointer = fopen ( $xml_file , 'r' );
while ( $xml_data = fread ( $file_pointer , 4096)) {
if (!xml_parse( $xml_parser , $xml_data , feof ( $file_pointer ))) {
die ( print "ERROR: " .
xml_error_string(xml_get_error_code( $xml_parser )) .
"<br>Error Code: " .
xml_get_error_code( $xml_parser ) .
"<br>Line: " .
xml_get_current_line_number( $xml_parser ) .
"<br>Column: " .
xml_get_current_column_number( $xml_parser ) .
"<br>Byte Index: " .
xml_get_current_byte_index( $xml_parser ) . "<br>"
);
}
}
xml_parser_free( $xml_parser );
?>
|
Output:
ERROR: Mismatched tag
Error Code: 76
Line: 7
Column: 13
Byte Index: 208
geeks.xml file:
XML
<? xml version = "1.0 encoding=" utf-8"?>
< user >
< username > user123 </ username >
< name > firstname lastname </ name >
< phone > +91-9876543210 </ phone >
< detail > I am John Doe. Live in Kolkata, India. </ detail >
</ user >
|
Program 2:
PHP
<?php
$xml_file = 'geeks.xml' ;
$xml_parser = xml_parser_create();
$file_pointer = fopen ( $xml_file , 'r' );
while ( $xml_data = fread ( $file_pointer , 4096)) {
if (!xml_parse( $xml_parser , $xml_data , feof ( $file_pointer ))) {
die ( print "ERROR: " .
xml_error_string(xml_get_error_code( $xml_parser )) .
"<br>Error Code: " .
xml_get_error_code( $xml_parser ) .
"<br>Line: " .
xml_get_current_line_number( $xml_parser ) .
"<br>Column: " .
xml_get_current_column_number( $xml_parser ) .
"<br>Byte Index: " .
xml_get_current_byte_index( $xml_parser ) . "<br>"
);
}
}
xml_parser_free( $xml_parser );
?>
|
Output:
ERROR: String not closed expecting " or '
Error Code: 34
Line: 1
Column: 36
Byte Index: 37
Reference: https://www.php.net/manual/en/function.xml-error-string.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...