<cfxml variable="students"> <school> <name>John Barrett</name> <school>University of Hawaii</school> <department>New Media</department> </school> </cfxml> <cffile action="write" file="#getDirectoryFromPath(getTemplatePath())#students.xml" output="#students#"> <!--- dump results---> <cfdump var="#students#">
This produces the file students.xml in the same directory, with the following results
<?xml version="1.0" encoding="UTF-8"?> <school> <name>John Barrett</name> <school>University of Hawaii</school> <department>New Media</department> </school>
Now the next is where I am strgugling, I would like to have a form, that will update this file.
an example form would be
Now I would like to take the form fields are update the students.xml file.
First beofre I can update the file with cffile I need to figure out:
* reading the XML data with ColdFusion
* finding the last node
* creating new nodes with the new data
I would really like to thanks @Clarkee21 on twitter for all his help tonight!
Looked on the CF Docs page, and I could not find an example to help complete this.
so using the XmlElemNew function should update the file. So I tried this tonight
<cffile action="read" file="#expandPath('./students.xml')#" variable="xmldoc"> <cfset mydoc = XmlParse(xmldoc)> <cfdump var="#mydoc#">
This reads the current xml doc students.xml and dumps the results
now to write to a file in ColdFusion you use cffile =”write” and there is a parameter for output, which will write that data there. Now there is no need to re write the whole file, just want to add a new entry in the form
<school> <name>new student</name> <school>new school</school> <department>new dept</department> </school>
the above is just dumy code, but you get the point.
In the past I have alway used cfquery to write the data to the database, and then create a file to convert the database records to a XML file. The CF Docs had a great example for this.
However, when looking to just simple updating the XML, I can’t seem to find one code example for Help. I alsays asked in CF Talk, and asked on the Adobe Foum, but no luck. Thanks to twitter looks like I will get some Help, thanks to @charlie21, and to Ray Camden!
It is so late here, and I need to get to sleep, but I think that the CF Docs should provide a good example on how to do something as basic as updating the XML like this.
Somebody at work should be a way in php in just a few lines. I love CF, and I always want to do things in CF. I must be missing something here, as it can’t be this difficult!
I would say to adobe that a good example that somebody can onto the docs and find an example to do this would be a good idea.
This is the first example that I have experienced using ColdFusion, where using CF didn’t turn something that was hard in other languages and made it trivial. comes to mind! this tags makes working with the database so easy, as in other languages it is difficult.