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
<?xmlversion="1.0"encoding="utf-8"?><root>Â Â Â Â<div> This is a div </div></root> - index.php
<?phpÂÂ// Create a new XMLReader instance$XMLReader=newXMLReader();ÂÂ// 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 browserecho$element->nodeValue;?> - Output:
This is a div
Example 2:
- data.xml
<?xmlversion="1.0"encoding="utf-8"?><body>Â Â Â Â<h1style="color:green; font-size:100px;">ÂÂ Â Â Â Â ÂzambiatekÂÂ Â Â Â</h1></body> - index.php
<?phpÂÂ// Create a new XMLReader instance$XMLReader=newXMLReader();ÂÂ// 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=newDOMDocument();ÂÂ// Append the child to the element$DOMDocument->appendChild($element);ÂÂ// Render the XML in browserecho$DOMDocument->saveXML(); - Output:
Reference: https://www.php.net/manual/en/xmlreader.expand.php




