Forum Moderators: coopster

Message Too Old, No Replies

PHP SimpleXML Set Values

         

username

12:11 am on Dec 3, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi all,
I recently visited [php.net...] to learn how to set a value in a particular php node. I loaded my xml file, and followed Example #9 Setting values. As simple as this seems, it sets the value, on the fly, but doesn't save the value to the XML file on the server.

To clarify, when I echo $xml->asXML(); ,the node is updated, but when you save the file from the server, the original value remains. I thought it was permissions, so I set the file and folder to 777, but this has no effect? Is this supposed to save the value, or am I missing something?

Thanks in advance.

penders

12:30 am on Dec 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



To clarify, when I echo $xml->asXML(); ,the node is updated, but when you save the file from the server, the original value remains.

You need to save the output from $xml->asXML() to your file (just as you are sending this output to the browser by echo'ing it). In order to do this you need to use PHP's file handling functions fopen(), fwrite() etc. The SimpleXML object itself does not do this for you, it just loads, it does not save. All manipulation of nodes/elements are done in memory.

The ouput from ->asXML() is just a plain old string. So there is no particular reason why this can't be written to a plain old text file.

You could try extending the SimpleXML object to add this functionality? Read way down the comments on the PHP: SimpleXML Functions - Manual [uk2.php.net] for some tips.

username

8:07 am on Dec 4, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks it was just eh fwrite and fopen elements that were needed. I guess this is slightly unclear to newbies with SimpleXML that the SimpleXML doesn't do this for you.