Codice PHP:
<?php
Class xmlsource{
private $xml;
private $root;
private $xpath;
private $doc;
function init($xml){
$this->xml=$xml;
$this->doc = new DOMDocument();
$this->doc->preserveWhiteSpace = false;
$this->doc->load($xml);
$this->xpath = new DOMXPath($this->doc);
$this->root = $this->doc->documentElement;
}
function findnodes($query){
return $this->xpath->query($query);
}
function delnodes($query){
$nodes = $this->findnodes($query);
foreach($nodes as $node){
$parent= $node->parentNode;
$parent->removeChild($node);
}
}
function save($fileName){
$this->doc->save($fileName);
}
}
try{
$contenuto = new xmlsource();
$contenuto->init('dati.xml');
$contenuto->delnodes('//img');
$contenuto->save('pippo.xml');
}
catch(DOMException $e){
echo $e->getMessage();
}
?>
DOM_NOT_FOUND Generato se oldnode non è un figlio di questo nodo.