Kozical Posted December 28, 2006 Share Posted December 28, 2006 Hi, I'm rather new to the XML world yet not entirely new to the world of coding. I've learned a tremendous amount about XML in the past few days. I still however cannot find much, if anything, about writing to an XML file. I can do this with ASP although my current host does not support ASP because they run FreeBSD, and it costs double the money I pay now to have it switched to Windows. I need to know if there is a way to write (append or insert) a node to an XML file. Say I want to make a message board for example and a cpanel. I need to be able to add and delete from the XML file so it keeps a current record of threads.(this is my crude example of adding to an xml file) <forum> <category name="PHP"> <post name="HELP ME"> <entry name="0001"> <author>Bob</author> <class>newbie</class> <posts>1</posts> <timeof>13:00GMT</timeof> <text>I need help making my php work </text> </entry> <entry name="0002"> <author>John</author> <class>Admin</class> <posts>1056</posts> <timeof>13:01GMT</timeof> <text>Please explain a bit better..</text> </entry> </post>( I want to add another entry here, and have the possibility of removing just that entry, "asin to move it to another category or just remove it from the forum altogether" ) </category> <category name="Private"> </category></forum> Is this a possibility with any other language except ASP?, I tried to make it work with perl, and php seems like it would take me writing an entire class/function set to get it to work properly. I've seen all sorts of (appendChild,removeChild) commands that look like they would work for this although I have yet to see any such commands in action in a formal tutorial or some sort of coding example in any such place. I would greatly appreciate any efforts or intriguing answers you may have. Link to comment Share on other sites More sharing options...
boen_robot Posted December 28, 2006 Share Posted December 28, 2006 Every language that supports DOM and/or has an XML parser is able to write to an XML file. This of course excludes JavaScript and VBScript as client side scripting languages. They can only read from an XML file.See the XML DOM tutorial in order to learn the principles of DOM. They are the same across languages. See the DOM functions in PHP to see what XML abilities PHP has.As for the fact that it would requre a whole function to work properly... I'm afraid that's true. You may ease the things up a little bit if you use an XSLT file that would copy the old file and insert the new contents, but that is not as efficient as DOM when you work with large files (in small files though, there's no difference, making everyone love the XSLT method).When XForms are supported in the browsers, things will be better. XForms generate an XML file. With PHP, you may then simply take that file, validate it (DTD, Schema, RelaxNG) and insert the complete tree with DOM on success. Link to comment Share on other sites More sharing options...
Kozical Posted December 30, 2006 Author Share Posted December 30, 2006 Thanks, I tried it, it dynamically adds the node. It doesn't change the xml file, I'm guessing that there is some sort of save function to write it back into the file? I tried to post the document to php then save with php but it didn't work out. Link to comment Share on other sites More sharing options...
boen_robot Posted December 31, 2006 Share Posted December 31, 2006 See the save() function from the DOM class.If that doesn't work, use file_put_contents(). Link to comment Share on other sites More sharing options...
Kozical Posted January 1, 2007 Author Share Posted January 1, 2007 Thanks again boen_robot, you definitely live up to the title XSLT Senior :-)I cannot as of yet test the code because I have php 4 installed on my webserver but, I'm sure you wouldn't steer me wrong with your suggestions. Thank you. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now