943,865 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 3733
  • PHP RSS
Aug 12th, 2009
0

Updating XML nodes using SimpleXML adds extra node

Expand Post »
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:
PHP Syntax (Toggle Plain Text)
  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:

PHP Syntax (Toggle Plain Text)
  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:

PHP Syntax (Toggle Plain Text)
  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>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Mapper99 is offline Offline
42 posts
since Apr 2008
Aug 12th, 2009
0

Re: Updating XML nodes using SimpleXML adds extra node

php Syntax (Toggle Plain Text)
  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:

php Syntax (Toggle Plain Text)
  1. $desc = "Penner";

I think that will be closer to what you are after.
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Aug 12th, 2009
0

Re: Updating XML nodes using SimpleXML adds extra node

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?
Reputation Points: 10
Solved Threads: 0
Light Poster
Mapper99 is offline Offline
42 posts
since Apr 2008

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: Strange array problem
Next Thread in PHP Forum Timeline: accessing individual elements





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


Follow us on Twitter


© 2011 DaniWeb® LLC