PHP | XMLReader readOuterXml() Function

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
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

  • index.php
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?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
    // Here it will include itself,
    // <h1> tags also
    echo "The text inside is:"
        $XMLReader->readOuterXml();
    ?>

    chevron_right

    
    

  • Output:

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

  • data.xml
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?xml version="1.0" encoding="utf-8"?>
    <div>
        <h1>Hello World 
          <sub>G4G</sub>
        </h1>
    </div>

    chevron_right

    
    

  • index.php
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?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->readOuterXml();
    ?>

    chevron_right

    
    

  • Output:

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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.