Change language

PHP Xml_parse () function

Syntax:
int xml_parse (resource $xml_parser, string $xml_data , bool $is_final)
Parameter:This function takes three parameters as above and described below:
  • $xml_parser:Required parameter. It defines the XML parser to be used.
  • $xml_data:Required. It defines the data to be analyzed.
  • $is_final:This is an optional parameter. If this parameter is set to True, then the data is the last chunk of data sent in this analysis.
Return Value:This function returns True on success or False on error.Notes :
  • This function is available for PHP 4.0.0 and newer.
  • These examples may not work in the online IDE. So, try running it on your local server or on the php hosts.
gfg.xml file: 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. & Lt; / detail > < / user > Program 1:  
// Create XML parser $parser = xml_parser_create();  
// Set up the character handler function
// for XML parser xml_set_character_data_handler ( $parser , "char_print" );  
// Opening the xml file $filePointer = fopen ( "gfg.xml" , "r" );  
// Read xml data from file while ( $data = fread ( $filePointer , 4096)) { // Parse XML data xml_parse ( $parser , $data , feof ( $filePointer )) or  // Display an error while parsing 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 ))); }  
// Free the xml parser xml_parser_free ( $parser ); fclose ( $filePointer );  
// Symbol 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:  
// Create xml parser $xml_parser = xml_parser_create();  
// Handler function for the element named “start_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 >
"
; break ; case "USERNAME" : echo "Username:" ; break ; case "NAME" : echo "Name:" ; break ; case "PHONE" : echo "Phone no:" ; break ; case "DETAIL" : echo "More about user:" ; } }  
// Handler function for the element named“ end_handler ” function ending_handler ( $xml_parser , $element_name ) { echo "
"
; }  
// Character handler function named char_handler function char_handler ( $xml_parser , $xml_data ) { echo $xml_data ; }  
// Install element handlers xml_set_element_handler ( $xml_parser , "starting_handler" , "ending_handler" );  
// Install the character data handler xml_set_character_data_handler ( $xml_parser , " char_handler " );  
// Opening the xml file $file_pointer = fopen ( "gfg.xml" , "r" );  
// Read the XML file while ( $xml_data = fread ( $file_pointer , 4096)) {   xml_parse ( $xml_parser , $xml_data , feof ( $file_pointer )) or // Display an error while parsing xml die (sprintf ( "XML Error :% s at line% d " , // Error line 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 );  
// Close the 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.
Link: https://www.php.net/manual/en/function.xml-parse.php

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method