$doc = new DOMDocument(); $doc->loadXML(''); $imgNode = $doc->getElementsByTagName('img')->item(0); $newNode = $doc->createTextNode('This is a text node.'); $imgParent = $imgNode->parentNode; $imgParent->replaceChild($newNode, $imgNode); echo $doc->saveXML();
$doc = new DOMDocument(); $doc->loadXML('In this example, we load an XML document into the DOMDocument object. We then select the div element and create a new paragraph element to replace it. Finally, we replace the old node with the new one and print the updated XML document. Package library: This method is a built-in method of the PHP DOM extension.'); $divNode = $doc->getElementsByTagName('div')->item(0); $pNode = $doc->createElement('p', 'New paragraph.'); $divParent = $divNode->parentNode; $divParent->replaceChild($pNode, $divNode); echo $doc->saveXML();Heading
Paragraph.