+

PHP Xml_parser_create () function

Syntax:
resource xml_parser_create (string $encoding)
Parameters:This function takes one parameter, $encoding,which is optional. Specifies character encoding:
  • for I / O in PHP 4
  • for output from PHP 5 only
  • for 5.0.0 and 5.0.1 output the default encoding is ISO-8859-1
  • from 5.0.2 the default encoding is UTF-8
Return value:this function returns a resource handler to be used by some other XML functions on success or False on error.Notes :
  • This function is available for PHP 4.0.0 and above.
  • 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. < / detail > < / user > Program 1:  
// Create XML parser $parser = xml_parser_create();  
// install the character handling function
// for XML parser xml_set_character_data_handler ( $parser , "char_print" );  
// Open 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 parsing error 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 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.
engineer.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. < / detail > < / user > Program 2:  
// Create an 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 $fp = fopen ( "engineer.xml" , "r" );  
// Read the XML file while ( $data = fread ( $fp , 4096)) {  < / code> xml_parse ( $parser , $data , feof ( $fp )) or // Display an error while parsing xml   die (sprintf ( "XML Error:% s at line% d" < / code> , // 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 ( $fp );  
?>
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-create.php
Get Solution for free from DataCamp guru