PHP | XMLReader close() Function

The XMLReader::close() function is an inbuilt function in PHP which is used to close the input of XMLReader object which is currently parsing.

Syntax:

bool XMLReader::close( void )

Parameters: This function doesn’t accepts any parameters.



Return Value: This function returns TRUE on success or FALSE on failure.

Below examples illustrate the XMLReader::close() function in PHP:

Program 1:

  • data.xml
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?xml version="1.0" encoding="utf-8"?>
    <root>
        <p> Hello world </p>
    </root>

    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');
      
    // Close the XML Reader before reading XML
    $XMLReader->close();
      
    // Move to the first node
    $XMLReader->read();
      
    // Read it as a string
    $string = $XMLReader->readString();
      
    // Output the string to the browser
    echo $string;
    ?>

    chevron_right

    
    

  • Output:
    // Blank because we closed the input of the XMLReader before reading XML

Program 2:

  • data.xml
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?xml version="1.0" encoding="utf-8"?>
    <body>
        <h1> GeeksforGeeks </h1>
    </body>

    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');
      
    // Move to the first node
    $XMLReader->read();
      
    // Read it as a string
    $string = $XMLReader->readString();
      
    // Output the string to the browser
    echo $string;
      
    // Close the XML Reader after reading XML
    $XMLReader->close();
    ?>

    chevron_right

    
    

  • Output:
    GeeksforGeeks

Reference: https://www.php.net/manual/en/xmlreader.close.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.