944,167 Members | Top Members by Rank

Ad:
Oct 1st, 2009
-1

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.
XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <users>
  2. <user>
  3. <fullname>PC1</fullname>
  4. <floor>4</floor>
  5. </user>
  6. <user>
  7. <fullname>PC2</fullname>
  8. <floor>3</floor>
  9. </user>
  10. </users>

Here is my code so far:

XML, XSLT and XPATH 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. I am just trying to remove "PC1"

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:

XML, XSLT and XPATH 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 == "PCID4") {
  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
http://www.freegooglewaveinvites.com
Reputation Points: 10
Solved Threads: 0
Light Poster
Mapper99 is offline Offline
42 posts
since Apr 2008
Jan 17th, 2010
0
Re: Delete XML nodes failing...
This is an absolutely perfect solution! Thanks Mapper!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
disinfor is offline Offline
2 posts
since Jan 2010
Feb 6th, 2010
0

Actually, this code does not work consistently!

Click to Expand / Collapse  Quote originally posted by disinfor ...
This is an absolutely perfect solution! Thanks Mapper!!
I have found that this code is unreliable. Try this and see. Here is the XML:
XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <users>
  2. <user>
  3. <fullname>1</fullname>
  4. <floor>4</floor>
  5. </user>
  6. <user>
  7. <fullname>2</fullname>
  8. <floor>3</floor>
  9. </user>
  10. <user>
  11. <fullname>3</fullname>
  12. <floor>3</floor>
  13. </user>
  14.  
  15. </users>

Run the code with this If statement:

XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. if ($task == "3") {

XML record #2 gets deleted instead of #3!!!

Resulting XML:

XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <users>
  2. <user>
  3. <fullname>1</fullname>
  4. <floor>4</floor>
  5. </user>
  6.  
  7. <user>
  8. <fullname>3</fullname>
  9. <floor>3</floor>
  10. </user>
  11.  
  12. </users>

Also, if any of the tags had no data, they get converted to short notation:
<floor></floor> gets changed to <floor/>

This is the strangest thing! Any idea why this is happening?

Thanks!

Mapper
Reputation Points: 10
Solved Threads: 0
Light Poster
Mapper99 is offline Offline
42 posts
since Apr 2008
Feb 9th, 2010
0
Re: Delete XML nodes failing...
Click to Expand / Collapse  Quote originally posted by Mapper99 ...
I have found that this code is unreliable.

Also, if any of the tags had no data, they get converted to short notation:
<floor></floor> gets changed to <floor/>

This is the strangest thing! Any idea why this is happening?

Thanks!

Mapper
After I posted the "works" post, it found it does exactly what you are saying. I couldn't figure out why it's doing what it's doing. I thought maybe it was a white-space issue. The reason the if ($task == "3") deletes record 2 is that the count starts at 0 and not 1, so that makes sense.

I did find another solution that deletes the correct entry. I can't remember where I found it, but I did not write it. So here it goes and it may be a little long, but I'll explain what each section is doing:

XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <form enctype="multipart/form-data" action="action.php" method="POST">
  2. <select name="caption">
  3.  
  4. <?php
  5. $imagess = simplexml_load_file('xml/file.xml');
  6. foreach ($imagess->pic as $pic) {
  7. foreach ($pic->caption as $caption) {
  8. echo "<option value='" . $caption . "'>" . $caption . "</option>";
  9. }
  10. }
  11. ?>
  12. </select>
  13. <input type="hidden" name="category" value="furniture" />
  14. <p><input type="submit" value="Delete Entry" /></p>
  15. </form>

The code above is my form that pulls the information I want from the XML (the <caption> node) and echoes it into a drop down select list. This form then submits to the furnituredelete.php which contains the XML removal code.

action.php:

XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1.  
  2. $desc = $_POST["caption"]; // gets the caption data from the form
  3. $category = $_POST["category"]; // gets the category data from the from. There are a couple if/else if statements.
  4.  
  5. if ($category == "furniture") { // if statement, pretty self-explanatory
  6.  
  7. $xmlfile = "xml/file.xml";
  8. $xmlstr = file_get_contents("$xmlfile");
  9. $xml = new simplexmlElement($xmlstr);
  10.  
  11. $count = 0; //this is important. it sets the count at 0 so it targets the correct node.
  12. foreach ($xml as $pic){
  13. if ($pic->caption == $desc){
  14. unset($xml->pic[$count]); break;
  15. }
  16. $count++; // This is the important line as it updates the node count.
  17. }
  18.  
  19. echo "<div style='padding:50px;'><p style='font-family:Arial, Helvetica, san-serif; color:#666; font-size:1em;'>" . $desc . " has been deleted.<br /><a href='home.php'>Back to home page</a></p><p><a href='furn.php'>Back to Furniture delete page</a></p>";
  20.  
  21.  
  22. $handle = fopen("$xmlfile", "w"); //opens the xml and sets it for writing
  23. fwrite($handle, $xml->asXML()); //overwrites file and saves
  24. fclose($handle); // closes xml file
  25.  
  26. exit;
  27. } else {}
  28. //code actually has 2 more if statements, but it's not necessary for this example.

Here's the XML:

XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="utf-8" standalone="yes"?>
  2. <imagess>
  3.  
  4. <pic>
  5. <images>images/furniture/2.gif</images>
  6. <tag>images/sold.png</tag>
  7. <caption>1950's vintage chair</caption>
  8. </pic>
  9. <pic>
  10. <images>images/furniture/3.gif</images>
  11. <tag>images/forsale.png</tag>
  12. <caption>celadon vintage velvet swivel chair</caption>
  13. </pic>
  14. <pic>
  15. <images>images/furniture/4.gif</images>
  16. <tag>images/forsale.png</tag>
  17. <caption>bamboo barrel chair </caption>
  18. </pic>
  19. <pic>
  20. <images>images/furniture/5.gif</images>
  21. <tag>images/sold.png</tag>
  22. <caption>ivory damasked armoire in aged french blue</caption>
  23. </pic>
  24.  
  25. </imagess>

Hopefully that helps you out. I'm pretty new at PHP and what not, but was able to adjust the code to get it to work for my needs.
Last edited by disinfor; Feb 9th, 2010 at 12:36 pm. Reason: update
Reputation Points: 10
Solved Threads: 0
Newbie Poster
disinfor is offline Offline
2 posts
since Jan 2010

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 XML, XSLT and XPATH Forum Timeline: How to increment a variable in XSLT
Next Thread in XML, XSLT and XPATH Forum Timeline: XSLT : Find the maximum column in a table





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


Follow us on Twitter


© 2011 DaniWeb® LLC