PHP | xml_parser_free() Function Last Updated : 18 Nov, 2022 Comments Improve Suggest changes Like Article Like Report Pre-requisite: XML BasicsThe 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 Comment More infoAdvertise with us Next Article PHP | xml_parser_free() Function G gekcho Follow Improve Article Tags : Web Technologies PHP PHP-function Similar Reads PHP | xml_parser_create() Function The xml_parser_create() function is an inbuilt function in PHP which is used to create an XML parser. Syntax:Â resource xml_parser_create( string $encoding ) Parameters: This function accepts single parameter $encoding which is optional. It specifies the character encoding:Â Â for input/output in PHP 3 min read PHP | xml_parser_create_ns() Function The xml_parser_create_ns() function is an inbuilt function in PHP which is used to create an XML parser with namespace support and returns the resource handle. Syntax:Â resource xml_parser_create_ns( string $encoding, string $separator ) Parameters: This function accepts two parameters as mentioned 2 min read PHP | xml_parse() Function The xml_parse() function is an inbuilt function in PHP which is used to parse XML document. Syntax:Â int xml_parse( resource $xml_parser, string $xml_data, bool $is_final ) Parameter: This function accepts three parameters as mentioned above and described below:Â Â $xml_parser: It is required paramet 3 min read PHP | parse_url() Function The parse_url() function is an inbuilt function in PHP which is used to return the components of a URL by parsing it. It parses an URL and return an associative array which contains its various components. Syntax: parse_url( $url, $component = -1 ) Parameters: This function accepts two parameters as 2 min read PHP | xml_parser_get_option() Function Pre-requisite: XML Basics The xml_parser_get_option() function is an inbuilt function in PHP which retrieves the options from an XML parser. Syntax: mixed xml_parser_get_option( resource $parser, int $specified_option ) Parameters: This function accepts two parameters as mentioned above and describe 2 min read PHP | xml_parser_set_option() Function Pre-requisite: XML Basics The xml_parser_set_option() function is an inbuilt function in PHP which is used to set the options in an XML parser. Syntax: bool xml_parser_set_option( resource $parser, int $specified_option, mixed $option_value) Parameters: This function accepts three parameters as ment 2 min read PHP | xml_parse_into_struct() Function The xml_parse_into_struct() function is an inbuilt function in PHP which is used to parse XML data into an array structure. The XML data are parsed into two parallel array structures, first one is index array that contains pointers to the location of the values in the value array and second one is v 4 min read PHP | xml_get_error_code() Function The xml_get_error_code() function is an inbuilt function in PHP which is used to return the error code generated by XML parser. Syntax:Â int xml_get_error_code( resource $xml_parser ) Parameters: This function accepts single parameter $xml_parser which is required. It specifies the XML parser which 3 min read PHP | simplexml_load_file() Function The simplexml_load_file() function is an inbuilt function in PHP which is used to convert the well-formed XML document into the given file to an object. Syntax: SimpleXMLElement simplexml_load_file( string $filename, string $class_name = "SimpleXMLElement", int $options = 0, string $ns = "", bool $i 2 min read PHP | XMLWriter openUri() Function The XMLWriter::openUri() function is an inbuilt function in PHP which is used to create a new XMLWriter using source URI for output. In simple words, this function decides how to output the XML to user, it can be through a browser or directly to a file. Syntax: bool XMLWriter::openUri( string $uri ) 2 min read Like