0

Hi guys i have a simple XML document :

<iPhone>
 <enAttente>
  <UDID>value</UDID>
  <adresse>value</adresse>
 </enAttente>
 <enAttente>
  <UDID>value</UDID>
  <adresse>value</adresse>
 </enAttente>
</iPhone>

I want to delete <enAttente> where <adresse> = myValue this is what i do :

$doc = new DOMDocument;   
$doc->load('./folder/myFile.xml');  
if ($doc)
{
    $domNodeList = $doc->getElementsByTagname('enAttente');

    for ($i = 0; $i < count($array); $i++)
    {
        foreach ($domNodeList as $domElement ) {
         $authors = $domElement->getElementsByTagName( "adresse" );
         $author = $authors->item(0)->nodeValue;

         if ($author == "myValue")
         {
             echo"Founded ! Delete this node ! -- ";
             // HOW CAN I DELETE this <enAttente> !!
         }
        }
    }

    $doc->saveXML();      
}

Can someone telle how to delete element when i have found the one to delete ? thank you very much !

Subin
  • 3,445
  • 1
  • 34
  • 63
  • Nope because i have seen this post but i couldn't managed to it ! thanks – Jacques Attali Dec 31 '13 at 17:47
  • can you show the exact code you used that "does not work"? – Floris Dec 31 '13 at 17:49
  • Well i have try so many things like : $domElement->removeChild($authors->item(0)); $author->parentNode->removeChild($author); $domElement->parentNode->removeChild($domElement); – Jacques Attali Dec 31 '13 at 17:51
  • Note that ``saveXML()`` doesn't write the file: http://www.php.net/manual/en/domdocument.savexml.php It just returns a string. – Bgi Dec 31 '13 at 17:52

2 Answers2

1

It seems to me that you need

$domElement->parentNode->removeChild($domElement);
Floris
  • 45,857
  • 6
  • 70
  • 122
0

Ok here is some working code:

<?php
$file = './folder/myFile.xml';
$doc = new DOMDocument;   
$doc->load($file);  
if ($doc)
{
    $domNodeList = $doc->getElementsByTagname('enAttente');

    foreach ($domNodeList as $domElement ) {
        $authors = $domElement->getElementsByTagName('adresse');
        $firstNode = $authors->item(0);

        $author = $firstNode->nodeValue;
        if ($author == "myValue")
        {
            echo "Founded ! Delete this node ! -- ";
            $doc->documentElement->removeChild($domElement);
        }
    }

    $doc->save($file);      
}
Bgi
  • 2,513
  • 13
  • 12