Current Location: Home> Function Categories> xml_set_processing_instruction_handler

xml_set_processing_instruction_handler

Setting up processing instructions (PI) handler
Name:xml_set_processing_instruction_handler
Category:XML parser
Programming Language:php
One-line Description:Set up the processing instruction handler.

Definition and usage

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.

Example

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

grammar

 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:

  • $parser - Variable containing the XML parser that calls the handler
  • $target - A variable containing the target of the processing instruction
  • $data - A variable that contains the data of the instruction
Similar Functions
Popular Articles