| By Jeffry Houser | Article Rating: |
|
| August 17, 2005 09:30 AM EDT | Reads: |
30,675 |
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.
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="http://coldfusion.sys-con.com/author/3566Houser.rss">
</cfhttp>
<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.
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(MyXML.rss.channel.item)#" index="CurrentLink">
<cfoutput>
<a href="#MyXML.rss.channel.item[CurrentLink].link.xmltext#">
#MyXML.rss.channel.item[CurrentLink].title.xmltext#
</a><br>
</cfoutput>
</cfloop>
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: http://livedocs.macromedia.com/coldfusion/7/
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: http://livedocs.macromedia.com/coldfusion/7/
htmldocs/00001520.htm#1209889 and the XMLTransform function is here: http://livedocs.macromedia.com/coldfusion/7/
htmldocs/00000673.htm#140093. This article should give you a good starting point for using XML in your applications.
Published August 17, 2005 Reads 30,675
Copyright © 2005 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
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 www.flextras.com . 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 www.jeffryhouser.com, check out his podcast at www.theflexshow.com or check out his company at www.dot-com-it.com.
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Adobe May Cooperate with Apple to Transplant Flash Player to iPhone
- Adobe Flex Developer Earns $100K in New York City
- Adobe LiveCycle Enterprise Suite 2 for Cloud Computing
- Adobe Betas Target RIAs and Cloud Computing
- Adobe Cans Another 9% of its Workforce
- Moyea DVD4Web Converter V2.0 Converts DVD to FLV Fast and Synchronously with Watermarks
- Adobe Fiddles with its Web Apps
- Adobe & Salesforce Cut Cloud Deal
- Hosting.com Launches ColdFusion 9 in the Cloud
- The Real Time Infrastructure Ultimatum
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Eval JavaScript in a Global Context
- Fig Leaf Software to Exhibit at Government IT Conference & Expo
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Is Microsoft as Free as Open Source?
- Adobe Reader Sued
- The Planet Named “Bronze Sponsor” of Cloud Computing Expo
- Microsoft Expression Web Has Got Game
- Adobe May Cooperate with Apple to Transplant Flash Player to iPhone
- Adobe Flex Developer Earns $100K in New York City
- Bruce Chizen Joins Voyager Capital as Venture Partner
- My Top Seven Wishes From Adobe MAX 2009
- The Next Programming Models, RIAs and Composite Applications
- Where Are RIA Technologies Headed in 2008?
- Constructing an Application with Flash Forms from the Ground Up
- AJAX World RIA Conference & Expo Kicks Off in New York City
- CFEclipse: The Developer's IDE, Eclipse For ColdFusion
- Personal Branding Checklist
- Adobe Flex 2: Advanced DataGrid
- Has the Technology Bounceback Begun?
- Building a Zip Code Proximity Search with ColdFusion
- i-Technology Viewpoint: We Need Not More Frameworks, But Better Programmers
- The Asynchronous CFML Gateway
- Web Services Using ColdFusion and Apache CXF























