<?xml version="1.0" encoding="utf-8"?>
<messages>
<message time="1248083538">
<name>Ben</name>
<email>Ben's Email</email>
<msg>Bens message</msg>
</message>
<message time="1248083838">
<name>John Smith</name>
<email>[email protected]</email>
<msg>Can you do this for me?</msg>
</message>
</messages>
Code (PHP)
<?php
$doc = new DOMDocument;
$doc->load('theFile.xml');
$thedocument = $doc->documentElement;
//this gives you a list of the messages
$list = $thedocument->getElementsByTagName('message');
//figure out which ones you want -- assign it to a variable (ie: $nodeToRemove )
$nodeToRemove = null;
foreach ($list as $domElement){
$attrValue = $domElement->getAttribute('time');
if ($attrValue == 'VALUEYOUCAREABOUT') {
$nodeToRemove = $domElement; //will only remember last one- but this is just an example :)
}
}
//Now remove it.
if ($nodeToRemove != null)
$thedocument->removeChild($nodeToRemove);
echo $doc->saveXML();
?>
Fatal error: Uncaught exception 'DOMException' with message 'Not Found Error' in C:\AppServ\www\ftp2.php:22 Stack trace: #0 C:\AppServ\www\ftp2.php(22): DOMNode->removeChild(Object(DOMElement)) #1 {main} thrown in C:\AppServ\www\ftp2.php on line 22