Open In App

PHP | xml_parse() Function

Last Updated : 05 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 parameter. It specifies the XML parser which is to be used.
  • $xml_data: It is required parameter. It specifies the data to be parsed.
  • $is_final: It is optional parameter. If the value of this parameter is set to True then the data is the last piece of data sent in this parse.

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
 
// Create an xml parser
$xml_parser = xml_parser_create();
 
// Element handler function named "starting_handler"
// enables custom manipulation for output
function starting_handler($xml_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($xml_parser, $element_name) {
    echo "<br>";
}
 
// Character handler function named "char_handler"
function char_handler($xml_parser, $xml_data) {
    echo $xml_data;
}
 
// Setting element handlers
xml_set_element_handler($xml_parser,
                "starting_handler", "ending_handler");
 
// Setting character data handler
xml_set_character_data_handler($xml_parser, "char_handler");
 
// Opening xml file
$file_pointer = fopen("gfg.xml", "r");
 
// Reading xml file
while ($xml_data = fread($file_pointer, 4096)) {
  
    xml_parse($xml_parser, $xml_data, feof($file_pointer)) or
     
    // Display error while xml parsing   
    die (sprintf("XML Error: %s at line %d",
         
        // Error string
        xml_error_string(xml_get_error_code($xml_parser)),
     
        // Error line number
        xml_get_current_line_number($xml_parser)));
}
 
// Free xml parser
xml_parser_free($xml_parser);
 
// Closing file stream
fclose($file_pointer);
 
?>


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-parse.php
 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads