DOM API helps to create XML document easily as well as provide easy access to load and modify existing documents. With the following XML we will load the file we just created a few minutes ago and then we will change the header test of the first h1
object:
<?php $uri = 'c:/abc.xml'; $document = new DOMDocument(); $document->loadHTMLFile($uri);// load the content of this URL as HTML $h1s = $document->getElementsByTagName("h1");//find all h1 elements $newText = $document->createElement("h1","New Heading");//created a //new h1 element $h1s->item(0)->parentNode->insertBefore($newText, $h1s->item(0));//insert before the existing h1 element $h1s->item(0)->parentNode->removeChild($h1s->item(1));//remove the //old h1 element echo $document->saveHTML();//display the content as HTML ?>
The output is shown below:
<...