HomeLanguagesPHP | XMLReader readInnerXml() Function

PHP | XMLReader readInnerXml() Function

The XMLReader::readInnerXml() function is an inbuilt function in PHP which is used to read the contents of the current node, including child nodes and markup.

Syntax:

string XMLReader::readInnerXml( void )

Parameters: This function doesn’t accept any parameters.

Return Value: This function returns the content of the current node as a string or empty string in case of failure.

Below examples illustrate the XMLReader::readInnerXml() 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
      
    // Create a new XMLReader instance
    $XMLReader = new XMLReader();
      
    // Open the XML file
    $XMLReader->open('data.xml');
      
    // Iterate through the XML nodes to
    // reach the h1 element
    $XMLReader->read();
    $XMLReader->read();
    $XMLReader->read();
      
    // Print the XML content
    echo "The text inside is:" 
        . $XMLReader->readInnerXml();
    ?>

    
    
  • Output:
    The text inside is: Hello World

Example 2: In this program, we will read the value of a element with sub-nodes.

  • data.xml




    <?xml version="1.0" encoding="utf-8"?>
    <div>
        <h1> neveropen <h2>World</h2></h1>
    </div>

    
    
  • index.php




    <?php
      
    // Create a new XMLReader instance
    $XMLReader = new XMLReader();
      
    // Open the XML file
    $XMLReader->open('data.xml');
      
    // Iterate through the XML nodes to
    // reach the h1 element
    $XMLReader->read();
    $XMLReader->read();
    $XMLReader->read();
      
    // Print the XML content
    echo "The text inside is:" .
        $XMLReader->readInnerXml();
    ?>

    
    
  • Output:

Reference: https://www.php.net/manual/en/xmlreader.readinnerxml.php

RELATED ARTICLES

Most Popular

Dominic
32714 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7088 POSTS0 COMMENTS
Nicole Veronica
12228 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12310 POSTS0 COMMENTS
Shaida Kate Naidoo
7204 POSTS0 COMMENTS
Ted Musemwa
7470 POSTS0 COMMENTS
Thapelo Manthata
7190 POSTS0 COMMENTS
Umr Jansen
7171 POSTS0 COMMENTS