xml_set_processing_instruction_handler
Setting up processing instructions (PI) handler
The xml_set_processing_instruction_handler()
function is used to specify the function to be called when the parser finds a processing instruction (PI) in an XML document.
Processing instructions (PIs) are included in <? and ?> and contain a target and its data.
Example: In this case, the PI associates a stylesheet with an XML document:
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="default.xsl" type="text/xml"?> < note > < to > Tove </ to > < from > Jani </ from > < heading > Reminder </ heading > < body > Don't forget me this weekend! </ body > </ note >
Note: The handler parameter can also be an array containing object references and method names.
Create an XML parser, set up a character data handler, set up a processing instruction (PI) handler, and parse the XML document (note_pi.xml):
<?php $parser = xml_parser_create ( ) ; function char ( $parser , $data ) { echo $data ; } function pi_handler ( $parser , $target , $data ) { echo "Target: $target <br />" ; echo "Data: $data <br />" ; } xml_set_character_data_handler ( $parser , "char" ) ; // Set processing instruction (PI) handler xml_set_processing_instruction_handler ( $parser , "pi_handler" ) ; $fp = fopen ( "note_pi.xml" , "r" ) ; while ( $data = fread ( $fp , 4096 ) ) { 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 ) ; ?>
Run the instance
xml_set_processing_instruction_handler ( parser , handler )
parameter | describe |
---|---|
parser | Required. Specifies the XML parser to use. |
Handler |
Required. Specifies the function used as the event handler. This function must accept three parameters:
|