
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
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