PHP xml_set_processing_instruction_handler() Function

❮ PHP XML Parser Reference

Example

Create an XML parser, set character data handler, set PI handler, and parse an 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 up 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 at line %d",
  xml_error_string(xml_get_error_code($parser)),
  xml_get_current_line_number($parser)));
}
xml_parser_free($parser);
?>
Run Example »

Definition and Usage

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

A PI is enclosed in <? and ?> and contains a a target followed by data.

Example: In this case the PI associates a style sheet 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 an object reference and a method name.

Syntax

xml_set_processing_instruction_handler(parser, handler)

Parameter Values

Parameter Description
parser Required. Specifies the XML parser to use
handler Required. Specifies a function to be used as an event handler. The function must accept three parameters:
  • $parser - A variable containing the XML parser calling the handler
  • $target - A variable containing the PI target
  • $data - A variable containing the PI data


Technical Details

Return Value: TRUE on success. FALSE on failure
PHP Version: 4.0+

❮ PHP XML Parser Reference
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.