| | |
Updating XML nodes using SimpleXML adds extra node
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Apr 2008
Posts: 30
Reputation:
Solved Threads: 0
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 Code:
The updated XML looks like this:
XML:
PHP Syntax (Toggle Plain Text)
<?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 Syntax (Toggle Plain Text)
<?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:
PHP Syntax (Toggle Plain Text)
<?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>
php Syntax (Toggle Plain Text)
$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)
$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. ![]() |
Similar Threads
- Database values in fopen path? (PHP)
- Passing XML (node) parameters to XSLT in Saxon (XML, XSLT and XPATH)
- Read/Edit specific .xml nodes in vb6 (Visual Basic 4 / 5 / 6)
- function to return parent node (C++)
- Updating a XML file (Java)
- XSLT to copy a specific number of nodes (XML, XSLT and XPATH)
- Word 2007 adds extra spaces in mail merge (Windows Software)
- Adding Attributes to all XML Nodes (RSS, Web Services and SOAP)
- Parse XML from ASP!!! (ASP)
Other Threads in the PHP Forum
- Previous Thread: Strange array problem
- Next Thread: accessing individual elements
| Thread Tools | Search this Thread |
apache api array beginner binary body broken cakephp checkbox class cms code cron curl database date date/time delete display dynamic echo email error file files filter folder form forms function functions gc_maxlifetime global google host href htaccess html image include insert ip javascript joomla limit link list login mail memmory memory menu mlm msqli_multi_query multiple mycodeisbad mysql navigation oop parameter parsing paypal pdf php problem query radio random recourse recursion regex remote script search seo server sessions sms snippet source space sql static syntax system table thesishelp trouble tutorial update upload url validator variable video web webdesign wordpress xml youtube





