PHP | XMLReader readOuterXml() Function
Last Updated :
18 Mar, 2020
The XMLReader::readOuterXml() function is an inbuilt function in PHP which is used to read the contents of the current node, including the node itself.
Syntax:
string XMLReader::readOuterXml( void )
Parameters: This function doesn’t accept any parameter.
Return Value: This function returns the contents of current node, including itself, as a string or empty string on failure.
Below examples illustrate the XMLReader::readOuterXml() function in PHP:
Example 1: In this program, we will read the value of an element without sub-nodes.
- data.xml
<? xml version = "1.0" encoding = "utf-8" ?>
< div >
< h1 >Hello World</ h1 >
</ div >
|
- index.php
<?php
$XMLReader = new XMLReader();
$XMLReader ->open( 'data.xml' );
$XMLReader ->read();
$XMLReader ->read();
$XMLReader ->read();
echo "The text inside is:" .
$XMLReader ->readOuterXml();
?>
|
- Output:
Example 2: In this program, we will read the value of an element with sub-nodes.
- data.xml
<? xml version = "1.0" encoding = "utf-8" ?>
< div >
< h1 >Hello World
< sub >G4G</ sub >
</ h1 >
</ div >
|
- index.php
<?php
$XMLReader = new XMLReader();
$XMLReader ->open( 'data.xml' );
$XMLReader ->read();
$XMLReader ->read();
$XMLReader ->read();
echo "The text inside is:" .
$XMLReader ->readOuterXml();
?>
|
- Output:
Reference: https://www.php.net/manual/en/xmlreader.readouterxml.php
Share your thoughts in the comments
Please Login to comment...