FredPenner Posted August 12, 2009 Share Posted August 12, 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: <?xml version="1.0" encoding="UTF-8" ?> <users> <user> <id>1</id> <firstname>Bob</firstname> <lastname>Marley</lastname> </user> <user> <id>2</id> <firstname>Bruce</firstname> <lastname>Springsteen</lastname> </user> </users> PHP Code: <?php $file = "officedata.xml"; $xml=simplexml_load_file($file); foreach ($xml->xpath('//user[id="2"]/lastname') as $desc) { echo "$desc\n"; $desc->lastname = "Penner"; } file_put_contents($file, $xml->asXML()); ?> The updated XML looks like this: <?xml version="1.0" encoding="UTF-8" ?> <users> <user> <id>1</id> <firstname>Bob</firstname> <lastname>Marley</lastname> </user> <user> <id>2</id> <firstname>Bruce</firstname> <lastname> Springsteen <lastname>Penner</lastname> </lastname> </user> </users> Link to comment https://forums.phpfreaks.com/topic/169929-updating-xml-nodes-using-simplexml-adds-extra-node/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.