2
    $xmldoc = new DOMDocument();
    $xmldoc->load('card.xml');
    $root   = $xmldoc->documentElement;
    $fnode  = $root->firstChild;
    // we retrieve the chapter and remove it from the book
    $items = $xmldoc->getElementsByTagName('card');
    foreach ($items as $item){
        $node = $item->getElementsByTagName('cardnumber')->item(0);
        if ($node->nodeValue == $this->cardnumber){
            $node->parentNode->removeChild($node);
            $xmldoc->saveXML();
        }
    }

Above is the code I used to delete the card node if the card number is match, below is my XML format. But if failed to remove the card. Can anyone help?

<root>
  <card>
    <cardnumber>12345</cardnumber>
    <name>Tan</name>
  </card>
  ....
</root>
4
  • why do you $xmldoc->saveXML(); inside the loop ? saveXML returns the xml string - what I guess you need is $xmldoc->save([file] http://www.php.net/manual/en/domdocument.save.php Commented Apr 23, 2013 at 10:21
  • I can do insert, now I want delete Commented Apr 23, 2013 at 10:22
  • I think essentially what @Adidi is saying is that you're not saving the file after deleting, so any changes you make are being forgotten. Commented Apr 23, 2013 at 10:23
  • This question really helped me - why did it get downvoted? Commented Dec 31, 2013 at 16:23

3 Answers 3

3

Delete <card> with id=2 with simplexml:

$xml = simplexml_load_string($x); // assume XML in $x
$i = count($xml) - 1; 

for ($i; $i >= 0; $i--) {   
    $card = $xml->card[$i];
    if ($card['id'] == "2") unset($xml->card[$i]);
}

see it working : http://codepad.viper-7.com/9cX1qR

Sign up to request clarification or add additional context in comments.

Comments

0

The way you have removed the node is correct. But you to save it in an xml after saveXml() or you can just print the new xml and check for the update you have done. And also as per your logic save should be done after the loop. (i.e) as follows,

$xmldoc = new DOMDocument();
$xmldoc->load('card.xml');
$root   = $xmldoc->documentElement;
$fnode  = $root->firstChild;
// we retrieve the chapter and remove it from the book
$items = $xmldoc->getElementsByTagName('card');
foreach ($items as $item){
    $node = $item->getElementsByTagName('cardnumber')->item(0);
    if ($node->nodeValue == $this->cardnumber){
        $node->parentNode->removeChild($node);            
    }
}
$xmldoc->save('newXmlFile.xml');

(OR)

print $xmldoc->saveXML();

1 Comment

Hi, if I want delete whole card instead that particular cardnumber node, so far it will only delete the cardnumber node instead whole card.
0
$xmlDoc->getElementsByTagName("nameNode")->item(0)->parentNode
    ->removeChild($xmlDoc->getElementsByTagName("nameNode")->item(0));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.