// Load the XML document from a file $xmlDoc = new DOMDocument(); $xmlDoc->load('example.xml'); // Get the parent node $parent = $xmlDoc->getElementsByTagName('parent')->item(0); // Get the child nodes with tag name 'child' $children = $parent->getElementsByTagName('child'); // Loop through each child node and remove it foreach ($children as $child) { $parent->removeChild($child); } // Save the modified XML document $xmlDoc->save('example.xml');
// Load the HTML document from a string $htmlDoc = new DOMDocument(); $htmlDoc->loadHTML('In this example, we load an HTML document from a string and remove the second child node (index 1) from the 'div' node in the document. Package/library: The DOMNode removeChild method is part of the PHP DOM extension, which is included in the PHP standard library.'); // Get the parent node $parent = $htmlDoc->getElementsByTagName('div')->item(0); // Get the second child node (index 1) $child = $parent->childNodes->item(1); // Remove the child node $parent->removeChild($child); // Save the modified HTML document echo $htmlDoc->saveHTML();First paragraph
Second paragraph