PHP xml_set_character_data_handler() function
Definition and Usage
The xml_set_character_data_handler() function establishes a character data handler.
This function specifies the function to be called when the parser finds character data in an XML file.
If the handler is successfully established, this function will return true; otherwise, it will return false.
syntax
xml_set_character_data_handler(parser,handler)
parameters | description |
---|---|
parser | required. Specifies the XML parser to be used. |
handler | required. Specifies the function used as an event handler. |
by handler The function specified by the parameter must have two parameters:
parameters | description |
---|---|
parser | required. Specifies a variable that contains the XML parser used to call the handler. |
data | required. Specifies a variable that contains character data. |
description
handler The parameter can also be an array containing object references and method names.
Example
XML File:
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
PHP Code:
<?php $parser=xml_parser_create(); function char($parser,$data) { echo $data; } xml_set_character_data_handler($parser,"char"); $fp=fopen("test.xml","r"); while ($data=fread($fp,4096)) { xml_parse($parser,$data,feof($fp)) or die (sprintf("XML Error: %s at line %d", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser))); } xml_parser_free($parser); ?>
Output:
George John Reminder Don't forget the meeting!