Change language

PHP Xml_set_character_data_handler () function

Syntax:
bool xml_set_character_data_handler (resource $xml_parser, callable $data_handler )
Parameters:This function takes two parameters as above and described below:
  • $xml_parser:required parameter. It contains a link to the XML parser for configuring the character data handler.
  • $data_handler:Required. This is a string that contains the name of the function.
    handler (resource $parser, string $data)
    The handler function must have these two parameters:
    • $xml_parser:contains a link to the XML parser that invokes the handler.
    • $data:contains character data as a string.
Returned value:This function returns True on success or False on error.Notes :
  • This function is available for PHP 4.0.0 and above.
  • These examples may not work online. the 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 $xml_parser = xml_parser_create();  
// Set up a character handler function for the XML parser xml_set_character_data_handler ( $xml_parser , "char_print" );  
// Opening the xml file $file_pointer = fopen ( "gfg.xml" , "r" );  
// Read xml data from file while ( $data = fread ( $file_pointer , 4096)) { // Parse XML data xml_parse ( $xml_parser , $data , feof ( $file_pointer )) or  // Display error on parse error die (sprintf ( "XML Error:% s at line% d" , // Error string xml_error_string (xml_get_error_code ( $xml_parser )), // Current line xml_get_current_line_number ( $xml_parser ))); }  
// Free xml parser xml_parser_free ( $xml_parser ); fclose ( $file_pointer );  
// Symbol handler function for XML parser function char_print ( $xml_parser , $data_to_print ) { echo $data_to_print ; }  
?>
Output:
user123 firstname lastname + 91-9876543210 I am John Doe. Live in Kolkata, India.
Program 2:  
// Create xml parser $xml_parser = xml_parser_create();  
// Handler function for the element named “start_handler”
// enables custom manipulation for output function starting_handler ( $xml_parser , $element_name , $element_attrs ) { switch ( $element_name ) { case "USER" : echo "< u > USER DATA < / u >
"
; 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 ( $xml_parser , $element_name ) { echo "
"
; }  
// Character handler function named char_handler function char_handler ( $xml_parser , $data ) { echo $data ; }  
// Install element handlers xml_set_element_handler ( $xml_parser , "starting_handler" , "ending_handler" );  
// Install the character data handler xml_set_character_data_handler ( $xml_parser , " char_handler " );  
// Opening the xml file $file_pointer = fopen ( "gfg.xml" , "r" );  
// Read the XML file while ( $data = fread ( $file_pointer , 4096)) { xml_parse ( $xml_parser , $data , feof ( $file_pointer ) ) or // Show error while parsing xml die (sprintf ( "XML Error:% s at line% d" , // Error string xml_error_string (xml_get_error_code ( $xml_parser )),   // Error line number xml_get_current_line_number ( $xml_parser ))); }  
// Free xml parser xml_parser_free ( $xml_parser );  
// Close the file stream fclose ( $file_pointer );  
?>
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-set-character-data-handler.php

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method