0

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.

simplyblue
  • 2,279
  • 8
  • 41
  • 67

2 Answers2

3

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

Shikiryu
  • 10,180
  • 8
  • 49
  • 75
  • Exactly! Use the $xml object to proof! – powtac Mar 07 '11 at 16:57
  • Just call the variable the same name as the root node, it will prevent that kind of mixups. – Josh Davis Mar 07 '11 at 17:55
  • @Josh Davis : It's a great advice for beginners :) – Shikiryu Mar 07 '11 at 18:21
  • Yeah, that's just about the best advice I can ever give to any SimpleXML user :) That, and also that they don't consider SimpleXMLElement as some kind as array that they can inspect with `print_r()` but that one is a bit too cryptic. – Josh Davis Mar 08 '11 at 15:47
-1

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

Community
  • 1
  • 1
Uku Loskit
  • 40,868
  • 9
  • 92
  • 93