simplyblue
simplyblue

Reputation: 2339

Remove a xml node in php

my xml file:

<temporary>
  <users>
    <temp>
      <id>1</id>
      <title> Undercover</title>
      <author>Wiwit</author>
    </temp>
    <nissi>
      <confirm>3977678bce8515e8cdbfa64850904ad1</confirm>
      <firstname>hi</firstname>
      <lastname>hhey</lastname>
      <day>1</day>
    </nissi>
   </users>
</temporary>

my php:

<?php
$user="nissi";
$xml = simplexml_load_file("temporary.xml") 
       or die("Error: Cannot create object");
unset($xml->temporary->users->$user);
?>

Why is this not working. The unset is not working.The node ins'nt getting deleted.

Upvotes: 0

Views: 2899

Answers (2)

Shikiryu
Shikiryu

Reputation: 10219

It works like this :

$user="nissi";
$xml = simplexml_load_file("temporary.xml") 
       or die("Error: Cannot create object");
unset($xml->users->$user);
echo $xml->asXML();

You mustn't take the root of your xml in the "request" temporary here.

DEMO HERE

Upvotes: 3

Uku Loskit
Uku Loskit

Reputation: 42040

You cannot do it with SimpleXML alone, you have to use DOMElement conversion as explained here:

Remove a child with a specific attribute, in SimpleXML for PHP

Upvotes: -1

Related Questions