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

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically