Sharing structured data

XML Magazine

Subscribe to XML Magazine: eMailAlertsEmail Alerts newslettersWeekly Newsletters
Get XML Magazine: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

XML Authors: Jason Bloomberg, Jayaram Krishnaswamy, Chris Pollach, Peter Silva, Mehdi Daoudi

Related Topics: ColdFusion on Ulitzer, XML Magazine

CFDJ: Article

Using XML in ColdFusion, XML is 8 Years Old

How many of you have been using it that long?

The cfxml tag contains two attributes:

  • Variable: The variable attribute defines the variable that will contain the resulting XML object. It is required.
  • CaseSensitive: The CaseSensitive attribute specifies whether the case used in the XML document should be maintained. It is an optional Boolean attribute with the default value of no.
Between the open cfxml tag and the close cfxml tag, specify the XML. For our example, I'm going to use some XML borrowed from the CFDJ RSS feed (see above for the link (see Listing 1).

I cut and pasted the XML directly from the CFDJ feed, then modified it to include fewer records. I set it to not be case sensitive, and stored the XML object in the variable "MyXML". The last line of code dumps the object. Inside the cfxml tags you can use any sort of ColdFusion processing that you need to create your XML object. For example, suppose you wanted to loop over the records of a database and use those records to create an XML object? We can do that with the CFXML tag.

XMLParse is a function used to turn a string into an XML object. Perhaps you are consuming a Web service that returns XML or using cfhttp to get an XML document. The XMLParse function has one argument, which is the string that contains the XML.

<cfhttp url="">

<cfset MyXML = XMLParse(cfhttp.FileContent)>

<cfdump var="#MyXML#">

The code grabs the CFDJ RSS feed using the cfhttp tag. The cfhttp.filecontent variable is a string. The XMLParse function is used to turn that string into an XML variable. The results dump the XML object to the screen, as shown in Figure 1.

Accessing the XML Object
Now that you've created an XML object, how do you use it within ColdFusion? This section will show you how. An XML object in ColdFusion is often a group of embedded structures and arrays. You can tell what's what by viewing the long version of the cfdump. Sometimes it can be tricky to parse this data because if its complexity. This is a list of elements that we'll need to access our data in the XML object:

  • XMLRoot: The XMLRoot is stored as an "XMLElement". You'll use this to access most of the data in your object. Each additional element I discuss here is part of the XML Element object. This keyword "XMLRoot" is interchangeable with the name of the top-level node. In the RSS feed, we could use "MyXML.xmlroot" or "myXML.rss".
  • XmlName: The name of the XML Element and it's a string.
  • XmlText: This is the content that resides between an open and close tag. It's a string.
  • XmlAttributes: The XMLAttributes is a structure. It contains the name, and values, of each attribute of the XML Element.
  • XmlChildren: XMLChildren is an array of all the children's elements. Each child is treated as an XML Element of its own.
A complete list of what is associated with an XML document in ColdFusion is located here htmldocs/00001510.htm#1119477. Table 1 shows how to access each line of the original XML file.

This is an example of what we would use to loop over the XML document and display a link to each article in the RSS feed:

<cfloop from="1" to="#arraylen(" index="CurrentLink">
  <a href="[CurrentLink].link.xmltext#">[CurrentLink].title.xmltext#

You've probably run this type of code many times against a ColdFusion query object. The code loops over the array of items. In the XML object items is an array of "XMLChildren". It displays a link using the "link" and displays the title as the link text, using a type of access similar to what is in the table.

Where to Go from Here
With this basic introduction to XML, where do you go from here? Well, you'll definitely want to read more about ColdFusion's XML functions: htmldocs/00000372.htm#3468770. You may want to investigate xPath to allow you to search through an XML object to find more information. XSL (Extensible Style sheets) are also intriguing. They allow you to transform an XML document using a style sheet to make it ready for Web display. ColdFusion supports XSL with the XMLTransform function. Information XSL is located here: htmldocs/00001520.htm#1209889 and the XMLTransform function is here: htmldocs/00000673.htm#140093. This article should give you a good starting point for using XML in your applications.

More Stories By Jeffry Houser

Jeffry is a technical entrepreneur with over 10 years of making the web work for you. Lately Jeffry has been cooped up in his cave building the first in a line of easy to use interface components for Flex Developers at . He has a Computer Science degree from the days before business met the Internet and owns DotComIt, an Adobe Solutions Partner specializing in Rich Internet Applications. Jeffry is an Adobe Community Expert and produces The Flex Show, a podcast that includes expert interviews and screencast tutorials. Jeffry is also co-manager of the Hartford CT Adobe User Group, author of three ColdFusion books and over 30 articles, and has spoken at various events all over the US. In his spare time he is a musician, old school adventure game aficionado, and recording engineer. He also owns a Wii. You can read his blog at, check out his podcast at or check out his company at

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.