Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP xml_set_element_handler() Function



The PHP XML Parser xml_set_element_handler() function is used to assign user-defined functions as handlers for an XML element's start and end tags. When processing XML files in PHP with the SimpleXML library or other XML parsing libraries, this function is used to define custom functions for handling an element's start and end tags.

The xml_set_element_handler() function is useful for modifying the contents of an XML element, like extracting specific data or applying changes to it.

Syntax

Below is the syntax of the PHP XML Parser xml_set_element_handler() function −

true xml_set_element_handler (
   XMLParser $xml_parser, 
   callable|string|null $start_handler, 
   callable|string|null $end_handler
   )

Parameters

Here are the parameters of the xml_set_element_handler() function −

  • $xml_parser: (Required) It is the XML parser on which the handlers are configured.

  • $start_handler: (Required) It is the name of the user-defined function that will handle an element's start tag.

  • $end_handler: (Required) It is the name of the user-defined function that will handle an element's end tag.

Return Value

The xml_set_element_handler() function returns TRUE on success. And FALSE on failure.

PHP Version

The xml_set_element_handler() function was introduced in core PHP 4 and is compatible with PHP 5, PHP 7, and PHP 8.

Example 1

First we will show you the basic example of the PHP XML Parser xml_set_element_handler() function. And we will see how this function sets user-defined methods for handling the start and end of XML elements during processing.

<?php
   // Define functions
   function startTag($p, $name) {
      echo "Start of element: $name\n";
   }
   function endTag($p, $name) {
      echo "End of element: $name\n";
   }

   // Initialize the XML parser
   $p = xml_parser_create();

   // Set the element handlers
   xml_set_element_handler($p, "startTag", "endTag");

   // Parse the XML data
   $data = "<note><to>Tanvi</to><from>Jain</from></note>";
   xml_parse($p, $data);

   // Free the parser
   xml_parser_free($p);
?>

Output

Here is the outcome of the following code −

Start of element: NOTE
Start of element: TO
End of element: TO
Start of element: FROM
End of element: FROM
End of element: NOTE

Example 2

In the below PHP code we will try to use the xml_set_element_handler() function and handle nested elements in the XML. The function is used to keep track of the nesting levels of XML elements and displays it in a hierarchical format with indents for clarity.

<?php
   // Define level variable
   $level = 0;

   // User-defined functions
   function startTag($p, $name) {
      global $level;
      echo str_repeat("-", $level) . "Start: $name\n";
      $level++;
   }
   function endTag($p, $name) {
      global $level;
      $level--;
      echo str_repeat("-", $level) . "End: $name\n";
   }

   // Initialize and configure parser
   $p = xml_parser_create();
   xml_set_element_handler($p, "startTag", "endTag");

   // XML data
   $data = "<root><item><subitem></subitem></item></root>";
   xml_parse($p, $data);

   // Free parser
   xml_parser_free($p);
?> 

Output

This will generate the below output −

Start: ROOT
-Start: ITEM
--Start: SUBITEM
--End: SUBITEM
-End: ITEM
End: ROOT

Example 3

This below PHP program extracts the data between the XML tags with the help of the xml_set_element_handler() function and prints it. This function helps the systematic processing of XML elements, allowing for operations like printing element names when their tags are present.

<?php
   // User-defined functions
   function startTag($p, $name) {
      echo "Start of element: $name\n";
   }
   function endTag($p, $name) {
      echo "End of element: $name\n";
   }
   function characterData($p, $data) {
      if (trim($data)) {
         echo "Data: $data\n";
      }
   }

   // Initialize parser and set handlers
   $p = xml_parser_create();
   xml_set_element_handler($p, "startTag", "endTag");
   xml_set_character_data_handler($p, "characterData");

   // Parse the XML data
   $data = "<book><title>PHP Basics</title><author>Tom Butler</author></book>";
   xml_parse($p, $data);

   // Free parser
   xml_parser_free($p);
?> 

Output

This will create the below output −

Start of element: BOOK
Start of element: TITLE
Data: PHP Basics
End of element: TITLE
Start of element: AUTHOR
Data: Tom Butler
End of element: AUTHOR
End of element: BOOK
php_function_reference.htm
Advertisements