ColdFusion & XML

<cfxml variable="students">
        <name>John Barrett</name>
        <school>University of Hawaii</school>
        <department>New Media</department>
<cffile action="write"
<!--- 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"?>
        <name>John Barrett</name>
        <school>University of Hawaii</school>
        <department>New Media</department>

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 ' + + ', This validation occurred after the CF validation.');

<!--- 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">
    <td><cfinput type="text" name="name" size="20" required="true" message="You must enter your name!" /></td>
    <td><cfinput type="text" name="school" size="20" required="true" message="You must enter your school!" /></td>
    <td><cfinput type="text" name="department" size="20" required="true" message="You must enter your department!" /></td>

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

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

        <name>new student</name>
        <school>new school</school>
        <department>new dept</department>

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.

One Reply to “ColdFusion & XML”

Leave a Reply

Your email address will not be published. Required fields are marked *