The XMLReader::read() function is an inbuilt function in PHP which is used to move to next node in document. Thus this function is used to traverse through the XML document.
Syntax:
bool XMLReader::read( void )
Parameters: This function doesn’t accepts any parameter.
Return Value: This function returns TRUE on success or FALSE on failure.
Below given programs illustrate the XMLReader::read() function in PHP:
Program 1: In this program, we will get the value of a element after traversing the file data.xml
Filename: data.xml
<? xml version = "1.0" encoding = "utf-8" ?>
< div1 >
< h1 > GeeksforGeeks </ h1 >
</ div1 >
|
Filename: index.php
<?php
$XMLReader = new XMLReader();
$XMLReader ->open( 'data.xml' );
$XMLReader ->read();
$XMLReader ->read();
$XMLReader ->read();
$XMLReader ->read();
echo "The text inside is: "
. "$XMLReader->value<br>" ;
?>
|
Output:
GeeksforGeeks
Program 2: In this program, we will get the name of an element after traversing to it.
Filename: data.xml
<? xml version = "1.0" encoding = "utf-8" ?>
< div1 >
< h1 > GeeksforGeeks </ h1 >
</ div1 >
|
Filename: index.php
<?php
$XMLReader = new XMLReader();
$XMLReader ->open( 'data.xml' );
$XMLReader ->read();
$XMLReader ->read();
$XMLReader ->read();
echo "The name of element is: "
. "$XMLReader->name<br>" ;
?>
|
Output:
The name of element is: h1
Reference: https://www.php.net/manual/en/xmlreader.read.php
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks 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!
Last Updated :
27 Mar, 2020
Like Article
Save Article