Friday, October 10, 2025
HomeLanguagesPHP | XMLReader expand() Function

PHP | XMLReader expand() Function

The XMLReader::expand() function is an inbuilt function in PHP which is used to copy the current node and returns the appropriate DOM object.

Syntax:

DOMNode XMLReader::expand( DOMNode $basenode )

Parameters: This function accepts a single parameter $basenode which holds a DOMNode defining the target DOMDocument for the created DOM object.

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

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

Example 1:

  • data.xml




    <?xml version="1.0" encoding="utf-8"?>
    <root>
        <div> This is a div </div>
    </root>

    
    
  • index.php




    <?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 element
    $element = $XMLReader->expand();
      
    // Print the node value to the browser
    echo $element->nodeValue;
    ?>

    
    
  • Output:
    This is a div

Example 2:

  • data.xml




    <?xml version="1.0" encoding="utf-8"?>
    <body>
        <h1 style="color:green; font-size:100px;"> 
          neveropen 
        </h1>
    </body>

    
    
  • index.php




    <?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 element
    $element = $XMLReader->expand();
      
    // Create a new DOMDocument instance
    $DOMDocument = new DOMDocument();
      
    // Append the child to the element
    $DOMDocument->appendChild($element);
      
    // Render the XML in browser
    echo $DOMDocument->saveXML();

    
    
  • Output:

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

RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS