Updating XML nodes using SimpleXML adds extra node

Reply

Join Date: Apr 2008
Posts: 31
Reputation: Mapper99 is an unknown quantity at this point 
Solved Threads: 0
Mapper99 Mapper99 is offline Offline
Light Poster

Updating XML nodes using SimpleXML adds extra node

 
0
  #1
Aug 12th, 2009
I have a very simple script that basically searches an xml file for a node based on the id of another node. I then update the node I found with a new value. For some reason my code is not performing the update. It is actually creating another node within the existing node. Any idea why?

XML:
  1.  
  2. <?xml version="1.0" encoding="UTF-8" ?>
  3. - <users>
  4. - <user>
  5. <id>1</id>
  6. <firstname>Bob</firstname>
  7. <lastname>Marley</lastname>
  8. </user>
  9. - <user>
  10. <id>2</id>
  11. <firstname>Bruce</firstname>
  12. <lastname>Springsteen</lastname>
  13. </user>
  14. </users>
PHP Code:

  1.  
  2. <?php
  3.  
  4. $file = "officedata.xml";
  5.  
  6. $xml=simplexml_load_file($file);
  7.  
  8. foreach ($xml->xpath('//user[id="2"]/lastname') as $desc) {
  9. echo "$desc\n";
  10. $desc->lastname = "Penner";
  11. }
  12.  
  13. file_put_contents($file, $xml->asXML());
  14.  
  15. ?>

The updated XML looks like this:

  1.  
  2. <?xml version="1.0" encoding="UTF-8" ?>
  3. - <users>
  4. - <user>
  5. <id>1</id>
  6. <firstname>Bob</firstname>
  7. <lastname>Marley</lastname>
  8. </user>
  9. - <user>
  10. <id>2</id>
  11. <firstname>Bruce</firstname>
  12. - <lastname>
  13. Springsteen
  14. <lastname>Penner</lastname>
  15. </lastname>
  16. </user>
  17. </users>
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 807
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Practically a Posting Shark

Re: Updating XML nodes using SimpleXML adds extra node

 
0
  #2
Aug 12th, 2009
  1. $desc->lastname = "Penner";

I believe your problem lies with this line of code. If you echo $desc to screen I think you will find that it relates to the lastname tag, not the user tag, so it is inserting a child of the lastname tag which is not what you want. If you change that line to this:

  1. $desc = "Penner";

I think that will be closer to what you are after.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 31
Reputation: Mapper99 is an unknown quantity at this point 
Solved Threads: 0
Mapper99 Mapper99 is offline Offline
Light Poster

Re: Updating XML nodes using SimpleXML adds extra node

 
0
  #3
Aug 12th, 2009
Thanks for your reply. When I change that line, the XML data is not updated at all. I believe that the simpleXML syntax for updating a node needs to have a certain format. Perhaps I am just setting a regular PHP variable using this syntax?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1086 | Replies: 2
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC