944,184 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 2707
  • PHP RSS
Oct 1st, 2009
0

Delete XML nodes failing...

Expand Post »
Hi there,

I am trying to delete XML nodes using PHP. Here is a sample of my xml file.
PHP Syntax (Toggle Plain Text)
  1. <users>
  2. <user>
  3. <fullname>PC1</fullname>
  4. <flooor>4</floor
  5. </user>
  6. <user>
  7. <fullname>PC2</fullname>
  8. <flooor>3</floor
  9. </user>
  10. </users>

Here is my code so far:

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $users = new DOMDocument();
  4. $users->load("officedata.xml");
  5. $suser = simplexml_load_file("officedata.xml");
  6. $count = 0;
  7.  
  8. foreach($suser->user as $user) {
  9.  
  10. if ($user['fullname'] == "PC1") {
  11. $users->documentElement->removeChild($users- documentElement->childNodes->item($count));
  12. $count--;
  13. }
  14. $count++
  15. }
  16. $users->save("officedata.xml");
  17.  
  18. ?>

Any idea why this won't work? If I remove the IF statement, all of the XML data gets removed.

Thanks in advance,

Mapper
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Mapper99 is offline Offline
42 posts
since Apr 2008
Oct 2nd, 2009
0

Re: Delete XML nodes failing...

For those of you could not figure this one out, after a few tries I got it:

PHP Syntax (Toggle Plain Text)
  1. $users = new DOMDocument();
  2. $users->load("officedata.xml");
  3.  
  4. $suser = simplexml_load_file("officedata.xml");
  5. $count = 0;
  6.  
  7. $user = $users->getElementsByTagName("user");
  8.  
  9. foreach($user as $value)
  10. {
  11. $count++;
  12. $tasks = $value->getElementsByTagName("fullname");
  13. $task = $tasks->item(0)->nodeValue;
  14.  
  15. if ($task == "PC1") {
  16. $users->documentElement->removeChild($users->documentElement->childNodes->item($count));
  17. }
  18. }
  19.  
  20. $users->save("officedata.xml");
  21. ?>

Works like a charm and is a very useful code snippet for deleting XML records using PHP!

Mapper
<FAKE SIGNATURE>
Last edited by peter_budo; Oct 2nd, 2009 at 10:34 am. Reason: Keep In The Site - No Fake signatures. Create a sitewide signature within the user control panel.
Reputation Points: 10
Solved Threads: 0
Light Poster
Mapper99 is offline Offline
42 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Multiple Files Upload to Server
Next Thread in PHP Forum Timeline: Time Format AM PM





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC