PHP | xml_set_character_data_handler() Function Last Updated : 07 Aug, 2021 Comments Improve Suggest changes Like Article Like Report The xml_set_character_data_handler() function is an inbuilt function in PHP which is used to set the character data handler function for XML parser. Syntax: bool xml_set_character_data_handler( resource $xml_parser, callable $data_handler ) Parameters: This function accepts two parameters as mentioned above and described below: $xml_parser: It is required parameter. It holds the reference of XML parser to set up character data handler.$data_handler: It is required parameter. It is a string which contains the name of function. handler( resource $parser, string $data ) The handler function must have these two parameters: $xml_parser: It holds the reference of XML parser which is calling to handler.$data: It holds the character data as a string. Return Value: This function returns True on success or False on failure. Note: This function is available for PHP 4.0.0 and newer version.These examples may not work on online IDE. So, try to run it on local server or php hosted servers. 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> </user> Program 1: PHP <?php // Create an XML parser $xml_parser = xml_parser_create(); // Set the character handler function for XML parser xml_set_character_data_handler($xml_parser, "char_print"); // Opening xml file $file_pointer = fopen("gfg.xml", "r"); // Reading xml data from file while($data = fread($file_pointer, 4096)) { // Parsing XML data xml_parse($xml_parser, $data, feof($file_pointer)) or // Display error when parsing error occurs 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 to xml parser xml_parser_free($xml_parser); fclose($file_pointer); // Character 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: PHP <?php // Create an xml parser $xml_parser = xml_parser_create(); // Element handler function named "starting_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><br>"; break; case "USERNAME": echo "Username: "; break; case "NAME": echo "Name: "; break; case "PHONE": echo "Phone no: "; break; case "DETAIL": echo "More about user: "; } } // Element handler function named "ending_handler" function ending_handler($xml_parser, $element_name) { echo "<br>"; } // Character handler function named "char_handler" function char_handler($xml_parser, $data) { echo $data; } // Setting element handlers xml_set_element_handler($xml_parser, "starting_handler", "ending_handler"); // Setting character data handler xml_set_character_data_handler($xml_parser, "char_handler"); // Opening xml file $file_pointer = fopen("gfg.xml", "r"); // Reading xml file while ($data = fread($file_pointer, 4096)) { xml_parse($xml_parser, $data, feof($file_pointer)) or // Display error while xml parsing 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 to xml parser xml_parser_free($xml_parser); // Closing 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. Reference: https://www.php.net/manual/en/function.xml-set-character-data-handler.php Comment More infoAdvertise with us Next Article PHP | xml_set_character_data_handler() Function G gekcho Follow Improve Article Tags : Web Technologies PHP PHP-function Similar Reads PHP | DOMCharacterData insertData() Function The DOMCharacterData::insertData() function is an inbuilt function in PHP which is used to insert a string at the specified 16-bit unit offset. Syntax: void DOMCharacterData::insertData( int $offset, string $data ) Parameters: This function accept two parameters as mentioned above and described belo 2 min read PHP | xml_parser_create() Function The xml_parser_create() function is an inbuilt function in PHP which is used to create an XML parser. Syntax:Â resource xml_parser_create( string $encoding ) Parameters: This function accepts single parameter $encoding which is optional. It specifies the character encoding:Â Â for input/output in PHP 3 min read PHP | DOMCharacterData replaceData() Function The DOMCharacterData::replaceData() function is an inbuilt function in PHP which is used to replace a substring within the DOMCharacterData node. Syntax: void DOMCharacterData::replaceData( int $offset, int $count, string $data) Parameters: This function accept three parameters as mentioned above an 2 min read PHP | xml_parser_create_ns() Function The xml_parser_create_ns() function is an inbuilt function in PHP which is used to create an XML parser with namespace support and returns the resource handle. Syntax:Â resource xml_parser_create_ns( string $encoding, string $separator ) Parameters: This function accepts two parameters as mentioned 2 min read PHP | DOMCharacterData substringData() Function The DOMCharacterData::substringData() function is an inbuilt function in PHP which is used to extracts a range of data from the node. Syntax: string DOMCharacterData::substringData( int $offset, int $count ) Parameters: This function accept two parameters as mentioned above and described below: $off 2 min read PHP | xml_parser_free() Function Pre-requisite: XML BasicsThe xml_parser_free() function is an inbuilt function in PHP which is used to free the XML parser. Syntax:Â Â bool xml_parser_free( resource $parser ) Parameters: This function accepts single parameter $parser which is required. It specifies the reference of XML parser to fre 3 min read PHP | DOMCharacterData appendData() Function The DOMCharacterData::appendData() function is an inbuilt function in PHP which is used to append the string to the end of the character data of the node. Syntax: public DOMCharacterData::appendData( string $data ) Parameters: This function accepts a single parameter $data which holds the string tha 2 min read PHP | DOMCharacterData deleteData() Function The DOMCharacterData::deleteData() function is an inbuilt function in PHP which is used to remove a range of characters from the node. Syntax: void DOMCharacterData::deleteData( int $offset, int $count ) Parameters: This function accept two parameters as mentioned above and described below: $offset: 1 min read PHP IntlChar::charName() Function PHP IntlChar::charName() function is an inbuilt function in PHP used to retrieve the name of a Unicode character. Syntax: string IntlChar::charName( $codepoint [, $nameChoice = IntlChar::UNICODE_CHAR_NAME] ) Parameters: This function accepts two parameters as mentioned above and described below: $co 2 min read PHP | xml_parser_set_option() Function Pre-requisite: XML Basics The xml_parser_set_option() function is an inbuilt function in PHP which is used to set the options in an XML parser. Syntax: bool xml_parser_set_option( resource $parser, int $specified_option, mixed $option_value) Parameters: This function accepts three parameters as ment 2 min read Like