PHP Function Reference

PHP xml_set_element_handler() Function



The PHP xml_set_element_handler() function sets the element handler functions for the XML parser. start_handler and end_handler are strings containing the names of functions that must exist when xml_parse() is called for parser.

Syntax

xml_set_element_handler(parser, start_handler, end_handler)

Parameters

parser Required. Specify a reference to the XML parser to set up start and end element handler functions.
start_handler Required. Specify a string containing the name of a function to be called at the start of an element. The function must have three parameters:
start_handler(parser, name, attribs)
  • parser: The first parameter, parser, is a reference to the XML parser calling the handler.
  • name: The second parameter, name, contains the name of the element for which this handler is called. If case-folding is in effect for this parser, the element name will be in uppercase letters.
  • attribs: The third parameter, attribs, contains an associative array with the element's attributes (if any). The keys of this array are the attribute names, the values are the attribute values. Attribute names are case-folded on the same criteria as element names. Attribute values are not case-folded.
Note: Instead of a function name, an array containing an object reference and a method name can also be used.
end_handler Required. Specify a string containing the name of a function to be called at the end of an element. The function must have two parameters:
end_handler(parser, name)
  • parser: The first parameter, parser, is a reference to the XML parser calling the handler.
  • name: The first parameter, name, contains the name of the element for which this handler is called. If case-folding is in effect for this parser, the element name will be in uppercase letters.
If a handler function is set to an empty string, or false, the handler in question is disabled.

Note: In XML, case-folding means uppercasing. By default, all the element names and element's attributes that are passed to the handler functions are case-folded. This behaviour can be queried and controlled per XML parser with the xml_parser_get_option() and xml_parser_set_option() functions, respectively.

Return Value

Returns true on success or false on failure.

Example: xml_set_element_handler() example

Lets assume that we have a file called test.xml. This file contains following content:

<?xml version="1.0" encoding="UTF-8"?>
<userlist>
  <user>
    <username>John123</username>
    <name>John Smith</name>
    <phone>+1-8054098000</phone>
    <address>Brooklyn, New York, USA</address>
  </user>
</userlist>  

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 and element handler functions. After parsing the document, the parser is freed using xml_parser_free() function.

<?php
//create an XML parser
$parser=xml_parser_create();

//element handler function named "start_handler"
//enables customized output
function start_handler($parser, $element, $attribs) {     
  switch($element) {
    case "USERNAME":
      echo "<b>User Name : "; 
      break;
    case "NAME":
      echo "Name : ";         
      break;
    case "PHONE":
      echo "Phone : ";        
      break;
    case "ADDRESS":
      echo "Address : ";      
      break;
  }
}
 
//element handler function "end_handler"
function end_handler($parser, $element) {
  if($element == "USERNAME") 
    echo "</b><br>";
  else
    echo "<br>";
}
 
// Setting element handlers
xml_set_element_handler($parser, "start_handler", 
                        "end_handler");

//character handler function for XML parser
function char_print($parser, $data) {
  echo $data;
}

//set the character handler function for XML parser
xml_set_character_data_handler($parser,"char_print");

//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);
?>

The output of the above code will be:

User Name : John123
Name : John Smith
Phone : +1-8054098000
Address : Brooklyn, New York, USA

❮ PHP XML Parser Reference