Friday, October 10, 2025
HomeLanguagesPHP | xml_parser_free() Function

PHP | xml_parser_free() Function

Pre-requisite: XML Basics
The xml_parser_free() function is an inbuilt function in PHP which is used to free the XML parser.

Syntax:  

bool xml_parser_free( resource $parser )

Parameters: This function accepts single parameter $parser which is required. It specifies the reference of XML parser to free.
Return Value: This function returns True on success or False on failure.

Note:  

  • This function is available for PHP 4.0.0 and newer version.
  • These examples may not work on online IDE. So, try to run it on local server or php hosted servers.

gfg.xml file:  

XML




<?xml version="1.0" encoding="utf-8"?>
<user>
    <username>user123</username>
    <name>firstname lastname</name>
    <phone>+91-9876543210</phone>
    <detail>I am John Doe. Live in Kolkata, India.</detail>
</user>


Program 1: 

PHP




<?php
 
// Create an XML parser
$parser = xml_parser_create();
  
// Set the character handler function
// for the XML parser
xml_set_character_data_handler($parser, "char_print");
  
// Opening xml file
$filePointer = fopen("gfg.xml", "r");
  
// Reading xml data from file
while ($data = fread($filePointer, 4096)) {
  
    // Parsing XML data
    xml_parse($parser, $data, feof($filePointer)) or
     
        // Display 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)));
}
     
// Freeing xml parser
xml_parser_free($parser);
     
fclose($filePointer);
  
// Character handler function for XML parser
function char_print($parser, $data) {
    echo $data;
}
  
?>


Output: 

user123 
firstname lastname 
+91-9876543210 
I am John Doe. Live in Kolkata, India. 

Program 2: 

PHP




<?php
 
// Creating an xml parser
$parser = xml_parser_create();
 
// Element handler function named "starting_handler"
// enables the custom manipulation for output
function starting_handler($parser,
                $element_name, $element_attrs) {
     
    switch($element_name) {
        case "USER":
            echo "<u>USER DATA</u><br>";
            break;
        case "USERNAME":
            echo "Username: ";
            break;
        case "NAME":
            echo "Name: ";
            break;
        case "PHONE":
            echo "Phone no: ";
            break;
        case "DETAIL":
            echo "More about user: ";
    }
}
 
// Element handler function named "ending_handler"
function ending_handler($parser, $element_name) {
    echo "<br>";
}
 
// Character handler function named "char_handler"
function char_handler($parser, $data) {
    echo $data;
}
 
// Setting element handlers
xml_set_element_handler($parser, "starting_handler",
                                  "ending_handler");
 
// Setting character data handler
xml_set_character_data_handler($parser, "char_handler");
 
// Opening xml file
$filePointer = fopen("gfg.xml", "r");
 
// Reading xml file
while( $data = fread($filePointer, 4096) ) {
  
    xml_parse($parser, $data, feof($filePointer)) or
     
        // Display error while xml parsing
        die (sprintf("XML Error: %s at line %d",
         
        // Error string
        xml_error_string(xml_get_error_code($parser)),
         
        // Error line number
        xml_get_current_line_number($parser)));
}
 
// Free to xml parser
xml_parser_free($parser);
 
// Closing file stream
fclose($filePointer);
 
?>


Output: 

USER DATA
Username: user123
Name: firstname lastname
Phone no: +91-9876543210
More about user: I am John Doe. Live in Kolkata, India.

Reference: https://www.php.net/manual/en/function.xml-parser-free.php
 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7101 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS