PHP xml_set_processing_instruction_handler() function

Definition and Usage

The xml_set_processing_instruction_handler() function defines the function to be called when the parser finds a processing instruction in the XML document.

The processing instruction is enclosed in <? and ?> delimiters.

If the handler is successfully established, the function will return true; otherwise, it will return false.

Example: In this example, the processing instruction associates a stylesheet with the XML document:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?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>

Syntax

xml_set_processing_instruction_handler(parser,handler)
Parameter Description
parser Mandatory. Defines the XML parser to be used.
handler Mandatory. Defines a function.

By handler The function defined by the parameter must have three parameters:

Parameter Description
parser Mandatory. Defines a variable that contains the XML parser used by the handler.
target Required. Specifies the variable that contains the processing instruction target.
data Required. Specifies the variable that contains the processing instruction data.

Description

handler The parameter can also be an array containing object references and method names.

Example

<?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");
xml_set_processing_instruction_handler($parser, "pi_handler");
$fp=fopen("test.xml","r");
while ($data=fread($fp,4096))
  {
  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);
?>