PHP Xml_parser_free () function

PHP

Function xml_parser_free() - this is a built-in function in PHP that is used to free the XML parser.Syntax:
bool xml_parser_free (resource $parser)
Parameters:This function takes one required parameter, $parser . Specifies a link to a free XML parser.Return Value:This function returns True on success or False on failure.Notes:
  • This feature is available for PHP 4.0.0 and later.
  • These examples may not work in the online IDE. So, try running it on your local server or on the php hosts.
gfg.xml file: 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. & Lt; / detail > < / user > Program 1:  
// Create XML parser $parser = xml_parser_create();  
// Set up the character handler function
// for XML parser xml_set_character_data_handler ( $parser , "char_print" );  
// Opening the xml file $filePointer = fopen ( "gfg.xml" , "r" );  
// Read xml data from file while ( $data = fread ( $filePointer , 4096)) { // Parse XML data xml_parse ( $parser , $data , feof ( $filePointer )) or  // Display an error while parsing die (sprintf ( " XML Error:% s at line% d " , // Error string xml_error_string (xml_get_error_code ( $parser )), // Current line xml_get_current_line_number ( $parser ))); }  
// Free the xml parser xml_parser_free ( $parser ); fclose ( $filePointer );  
// Symbol handler function for XML parser function char_print ( $parser , $data ) { echo $data ; }  
?>
Output:
user123 firstname lastname + 91-9876543210 I am John Doe. Live in Kolkata, India.
Program 2:  
// Create xml parser $parser = xml_parser_create();  
// Handler function for the element named “start_handler”
// enables custom manipulation for output function starting_handler ( $parser , $element_name , $element_attrs ) { switch ( $element_name ) { case "USER" : echo "< u > USER DATA < / u > < br >" ; break ; case "USERNAME" : echo "Username:" ; break ; case "NAME" : echo "Name:" ; break ; case "PHONE" : echo "Phone no:" ; break ; case "DETAIL" : echo "More about user:" ; } }  
// Handler function for the element named“ end_handler ” function ending_handler ( $parser , $element_name ) { echo "< br >" ; }  
// Character handler function named char_handler function char_handler ( $parser , $data ) { echo $data ; }  
// Install element handlers xml_set_element_handler ( $parser , "starting_handler" , "ending_handler" );  
// Install the character data handler xml_set_character_data_handler ( $parser , " char_handler " );  
// Opening the xml file $filePointer = fopen ( "gfg.xml" , "r" );  
// Read the XML file while ( $data = fread ( $filePointer , 4096)) {   xml_parse ( $parser , $data , feof ( $filePointer )) or // Display an error while parsing xml die (sprintf ( " XML Error:% s at line% d " , // Error line xml_error_string (xml_get_error_code ( $parser )), // Error line number xml_get_current_line_number ( $parser ))); }  
// Free xml parser xml_parser_free ( $parser );  
// Close the file stream fclose ( $filePointer );  
?>
Output:
USER DATA Username: user123 Name: firstname lastname Phone no: + 91-9876543210 More about user: I am John Doe. Live in Kolkata, India.
Link: https://www.php.net/manual/en/function.xml-parser-free.php



Get Solution for free from DataCamp guru