PHP xml_set_end_namespace_decl_handler() Function
The PHP xml_set_end_namespace_decl_handler() function sets end namespace declaration handler function for the given XML parser. This will be called, for each namespace declaration, after the handler for the end tag of the element in which the namespace was declared.
Syntax
xml_set_end_namespace_decl_handler(parser, handler)
Parameters
parser |
Required. Specify a reference to the XML parser to set up end namespace declaration handler function. |
handler |
Required. Specify a string containing the name of a function to be used as an event handler. The function must accept two parameters:
handler(parser, prefix)
|
Return Value
Returns true on success or false on failure.
Example: xml_set_end_namespace_decl_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 start_ns and end_ns functions are set as start and end namespace declaration handler function respectively for 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; } //start namespace declaration handler function for XML parser function start_ns($parser, $prefix, $uri) { echo "$prefix $uri \n"; } //end namespace declaration handler function for XML parser function end_ns($parser, $prefix) { echo "$prefix \n"; } //set the character handler function for XML parser xml_set_character_data_handler($parser,"char_print"); //set the start and end namespace declaration //handler function for XML parser xml_set_start_namespace_decl_handler($parser, "start_ns"); xml_set_end_namespace_decl_handler($parser, "end_ns"); //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