PHP xml_set_character_data_handler() Function

❮ PHP XML Parser Reference

Example

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

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

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

// Set the character data handler
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 at line %d",
  xml_error_string(xml_get_error_code($parser)),
  xml_get_current_line_number($parser)));
}

xml_parser_free($parser);
fclose($fp);
?>
Run Example »

Definition and Usage

The xml_set_character_data_handler() function sets the character data handler for the XML parser.

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

Note: The handler parameter can also be an array containing an object reference and a method name.

Syntax

xml_set_character_data_handler(parser, handler)

Parameter Values

Parameter Description
parser Required. Specifies the XML parser to use
handler Required. Specifies a function to be used as an event handler. The function must have two parameters:
  • $parser - A variable containing the XML parser calling the handler
  • $data - A variable containing the character data from the XML file as a string


Technical Details

Return Value: TRUE on success. FALSE on failure
PHP Version: 4.0+

❮ PHP XML Parser Reference
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.