ColdFusion & XML

//ColdFusion & XML
<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


<!--- Simple JavaScript Sample --->
<script language="javascript">
    function checkFields(frm) {
        alert('Dear ' + frm.name.value + ', This validation occurred after the CF validation.');
    }
</script>

<!--- Basic ColdFusion Form --->
<p>All Fields are required!</p>
<cfform action="form_action.cfm" method="post" name="myForm" onSubmit="return checkFields(document.myForm)">

<table width="200" border="1">
  <tr>
    <td>Name:</td>
    <td><cfinput type="text" name="name" size="20" required="true" message="You must enter your name!" /></td>
  </tr>
  <tr>
    <td>School</td>
    <td><cfinput type="text" name="school" size="20" required="true" message="You must enter your school!" /></td>
  </tr>
   <tr>
    <td>Department</td>
    <td><cfinput type="text" name="department" size="20" required="true" message="You must enter your department!" /></td>
  </tr>
</table>

    <cfinput type="submit" name="submit" value="submit" />
</cfform>

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

Update

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.

By | 2010-01-28T22:22:16+00:00 January 28th, 2010|ColdFusion|1 Comment

About the Author:

One Comment

  1. Lonnie Quirindongo January 19, 2012 at 9:38 am

    Very interesting information!Perfect just what I was looking for!

Leave A Comment