Well.. I ran across this code and wanted to share. I am using it as more of an example of something you probably shouldn't do.

One of the strengths of ColdFusion is to make hard things easy. This code basically ignores that principal and makes hard things harder.

So.. here is the code I found

view plain print about
1<cffile action="WRITE" file="#WallXML#\#NewFile#" output="<?xml version='1.0' encoding='UTF-8'?>" addNewLine="yes" fixnewline="yes" charset="iso-8859-1">
2
3
4<cffile action="APPEND" file="#WallXML#\#NewFile#" output="<gallery xmlns:media='http://search.yahoo.com/mrss/'>" addNewLine="yes" fixnewline="yes">
5
6<cffile action="APPEND" file="#WallXML#\#NewFile#" output="<settings>" addNewLine="yes" fixnewline="yes">
7    <cffile action="APPEND" file="#WallXML#\#NewFile#" output="<mediaFolder type='large' media='image'>media/3dwall/</mediaFolder>" addNewLine="yes" fixnewline="yes">
8    <cffile action="APPEND" file="#WallXML#\#NewFile#" output="<mediaFolder type='thumbnail' media='image'>media/3dwall/thumbs/</mediaFolder>" addNewLine="yes" fixnewline="yes">
9<cffile action="APPEND" file="#WallXML#\#NewFile#" output="</settings>" addNewLine="yes" fixnewline="yes">
10
11    <cfloop query="WallPics">
12            <cffile action="APPEND" file="#WallXML#\#NewFile#" output="<item>" addNewLine="yes" fixnewline="yes">
13                <cffile action="APPEND" file="#WallXML#\#NewFile#" output="<media:text>Tooltip 1</media:text>" addNewLine="yes" fixnewline="yes">
14                <cffile action="APPEND" file="#WallXML#\#NewFile#" output="<media:content url='#Photo#' type='image/jpeg' width='500' height='375' />" addNewLine="yes" fixnewline="yes">
15            <cffile action="APPEND" file="#WallXML#\#NewFile#" output="</item>" addNewLine="yes" fixnewline="yes">    
16    </cfloop>
17
18
19<cffile action="APPEND" file="#WallXML#\#NewFile#" output="</gallery>" addNewLine="yes" fixnewline="yes">

While in principal the code works. An XML file is generated and written to the server. However there are many issues with this.

1: The XML being written replaced an XML document used by a flash object. If the flash object tried to call the XML while it was being written it would error.

2: IF there was an error in XML generation the system would be left with an invalid and incomplete XML document on the server. This would probably cause the flash object to fail.

3: Causes excess I/O to the file system.

So, how did I fix it? Well easy.. ColdFusion has a built in way to create XML. There are a couple ways to do it but I used the tag approach. It just seemed to fit with the current code.

Here is what I ended up with:

view plain print about
1<cfoutput>
2<cfxml variable="theResult">
3<gallery xmlns:media='http://search.yahoo.com/mrss/'>
4    <settings>
5        <mediaFolder type='large' media='image'>media/3dwall/</mediaFolder>
6        <mediaFolder type='thumbnail' media='image'>media/3dwall/thumbs/</mediaFolder>
7    </settings><cfloop query="WallPics">
8        <item>
9            <media:text>Tooltip 1</media:text>
10            <media:content url='#Photo#' type='image/jpeg' width='500' height='375' />
11        </item></cfloop>
12</gallery>
13</cfxml>
14</cfoutput>
15
16<cffile action="write" file="#WallXML#\#NewFile#" output="#theResult#">

Now the code just generates a single XML var and then writes it at once to the server. If something were to go wrong with the generation the existing XML document will be left untouched. Also, since it is a single write the odds of the flash object being impacted by the new XML are drastically reduced.

So, remember, let ColdFusion do what it does. Don't try and work against it and make your lives harder.

Till next time,

--Dave