PHP | DOMNode removeChild() Function

The DOMNode::removeChild() function is an inbuilt function in PHP which is used remove child from list of children.

Syntax:

DOMNode DOMNode::removeChild( DOMNode $oldnode )

Parameters: This function accepts a single parameter $oldnode which holds the child to remove.

Return Value: This function returns the removed child on success.

Exceptions: This function throws DOM_NO_MODIFICATION_ALLOWED_ERR, if the node is readonly and DOM_NOT_FOUND, if $oldnode is not a child of this node.



Below examples illustrate the DOMNode::removeChild() function in PHP:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new DOMDocument instance
$document = new DOMDocument();
  
// Create a div element
$element = $document-> 
        appendChild(new DOMElement('div'));
  
// Create a text Node
$text1 = $document->
          createTextNode('GeeksforGeeks');
  
// Append the nodes
$element->appendChild($text1);
  
// Remove the child
$element->removeChild($text1);
  
// Render the XML
echo $document->saveXML();
?>

chevron_right


Output: Press Ctrl+U to see the XML

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new DOMDocument instance
$document = new DOMDocument();
  
// Create a h1 element
$element = $document->
             appendChild(new DOMElement('h1'));
  
// Create the text Node
$text1 = $document->
               createTextNode('GeeksforGeeks');
$text2 = $document->
          createTextNode('Text to be removed');
  
// Append the nodes
$element->appendChild($text1);
$element->appendChild($text2);
  
// Remove the child
$element->removeChild($text2);
  
// Render the output
echo $document->saveXML();
?>

chevron_right


Output:

Reference: https://www.php.net/manual/en/domnode.removechild.php




My Personal Notes arrow_drop_up

I love Open Source Culture have contributed to phpMyAdmin, LibreOffice, Ant-Design and Web is my passion

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.