Saturday 04 Jul 2009   
Add Comment

Generate XML Tutorial

ColdFusion Tutorial #20

As of ColdFusion MX, there are several tags and functions that make working with XML a snap. In this tutorial I am going to demonstrate how to use these tools to generate an XML document.

demo.cfm

We are going to use ColdFusion to export a list of books from the cfbookclub database. The first step is to write a query to get the data we want to export. (Lines 1-5)

Now that we have our data, we want to convert it into an XML Document, and as is often the case in programming, there is more than one way to accomplish this task. ColdFusion offers two basic methods for creating XML documents and which one you choose really depends on what you like better.

The first way I will show you is to use the CFXML tag which will procees the CFML between its start and end tags, then saves the output as a ColdFusion XML Document Object. This is the method I prefer to use because as you can see on lines 7-22 of the code it is much like what we do to generate HTML output for browsers, so I find it easier to work with than the alternative.

The second way (Lines 32 to 57) is to use xmlNew() to create a ColdFusion XML Document Object, then use XMLElemNew() to creade the neccessary XML nodes. You will see from the example that each node has a XMLChildren property which contains its child nodes and works like a ColdFusion array, so you can use array related functions to interact with it (see line 46). Each node also has an XMLAttributes property that you can interact with like you would a ColdFusion structure (line 43). Additionally each node has a XMLText property which you can use to set the text that goes between that nodes start and end tags (line 47).

As you can see from the results of the cfdumps, either of these methods will result in the same output, so you can pick whichever is more comfortable for you. You can use the toString() function to convert the XML document object to an XML string that can be saved as a file, inserted into a databse, submitted to an API, or whatever you need to do with it.

 
 


Demo

See this code running!


Download

Download this code as a zip!


Comments

You should always be using XmlFormat() when writing values out as XML, otherwise you can end up invalid XML due to characters like & not being escaped properly
zac spitzer @ Monday 26 Nov 2007 - 06:39:31 PM

did you know every tutorial zip file download link is the same tutorial?
when i hover over the link - the tutorial # is correct, but when i click it it's the same tutorial inside the zip.
Greg @ Thursday 14 Feb 2008 - 11:24:37 PM

Thanks for pointing this out, it was a bug and has been fixed.
Dale Fraser @ Saturday 16 Feb 2008 - 01:07:55 PM

an alternative way to produce xml from database query results is available in spry's documentation. check querytoxml. i prefer the automatic method. the cfloop automatically come up with the nodes and whatnot.
Amal @ Sunday 07 Sep 2008 - 07:39:01 PM

Click button to add a comment


Author

Scott Bennett


Published

Friday 23 Nov 2007

Tags

cfdump  cfloop  cfoutput  cfquery  cfset  cfxml