PHP Function Reference

PHP xml_set_external_entity_ref_handler() Function



The PHP xml_set_external_entity_ref_handler() function sets the external entity reference handler function for the given XML parser.

Syntax

xml_set_external_entity_ref_handler(parser, handler)

Parameters

parser Required. Specify a reference to the XML parser to set up external entity reference handler function.
handler Required. Specify a string containing the name of a function to be used as an event handler. The function must accept five parameters:
handler(parser, open_entity_names, base, system_id, public_id)
  • parser: The first parameter, parser, is a reference to the XML parser calling the handler.
  • open_entity_names: A space-separated list of the names of the entities that are open for the parse of this entity (including the name of the referenced entity).
  • base: The base for resolving the system identifier (system_id) of the external entity. Currently this parameter will always be set to an empty string.
  • system_id: The system identifier as specified in the entity declaration.
  • public_id: The public identifier as specified in the entity declaration, or an empty string if none was specified; the whitespace in the public identifier will have been normalized as required by the XML spec.
If a handler function is set to an empty string, or false, the handler in question is disabled. Note: Instead of a function name, an array containing an object reference and a method name can also be used.

Return Value

Returns true on success or false on failure.

Example: xml_set_external_entity_ref_handler() example

Lets assume that we have a file called test.xml. In the example below, an XML parser is created using xml_parser_create() function. Then the XML file is opened to parse its data using character handler function char_print. Then the ext_ent_handler function is set as external entity reference handler for the given XML parser. After parsing the document, the parser is freed using xml_parser_free() function.

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

//character handler function for XML parser
function char_print($parser, $data) {
  echo $data;
}

//external entity reference handler function for XML parser
function ext_ent_handler($parser, $not, $base, $sysID, $pubID) {
  echo "$not $sysID $pubID";
}

//set the character handler function for XML parser
xml_set_character_data_handler($parser,"char_print");

//set the external entity reference handler for XML parser
xml_set_external_entity_ref_handler($parser,"ext_ent_handler");

//opening xml file
$fp = fopen("test.xml", "r");

while($data = fread($fp,4096)) {
  //parsing XML data
  xml_parse($parser,$data,feof($fp)) or

    //displaying error when parse error occurs
    die (sprintf("XML Error: %s at line %d",

    //error string
    xml_error_string(xml_get_error_code($parser)),

    //current line
    xml_get_current_line_number($parser)));
}

//free XML parser
xml_parser_free($parser);

fclose($fp);
?>

❮ PHP XML Parser Reference