Current Location: Home> Function Categories> xml_set_character_data_handler

xml_set_character_data_handler

Setting up character data processor
Name:xml_set_character_data_handler
Category:XML parser
Programming Language:php
One-line Description:Set up a character data handler for the XML parser.

Definition and usage

xml_set_character_data_handler() function is used to set up a character data handler for the XML parser.

This function specifies the function to be called when the parser finds character data in the XML file.

Note: The handler parameter can also be an array containing object references and method names.

Example

Create an XML parser, set up a character data handler, and parse the XML document ( note.xml ):

 <?php
// Create XML parser
$parser = xml_parser_create ( ) ;

function char ( $parser , $data ) {
  echo $data ;
}

// Set character data processing program
xml_set_character_data_handler ( $parser , "char" ) ;

$fp = fopen ( "note.xml" , "r" ) ;

while ( $data = fread ( $fp , 4096 ) ) {
  // parse XML data
  xml_parse ( $parser , $data , feof ( $fp ) ) or
  die ( sprintf ( "XML error: %s on line %d" ,
  xml_error_string ( xml_get_error_code ( $parser ) ) ,
  xml_get_current_line_number ( $parser ) ) ) ;
}

xml_parser_free ( $parser ) ;
fclose ( $fp ) ;
?>

Run the instance

grammar

 xml_set_character_data_handler ( parser , handler )
parameter describe
parser Required. Specifies the XML parser to use.
Handler

Required. Specifies the function used as the event handler. The function must have two parameters:

  • $parser - Variable containing the XML parser that calls the handler
  • $data - A string variable containing character data from an XML file
Similar Functions
Popular Articles