YOUR FEEDBACK
Two great PDF creators
Michael Jahn wrote: related to the snapscan - are their an samples of the ...
SOA World Conference
Virtualization Conference
$50 Savings Expire May 23, 2008... – Register Today!


2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TOP COLDFUSION LINKS


Searching through your iTunes Library using ColdFusion
Accessing XML data in ColdFusion

Digg This!

Page 1 of 2   next page »

Unless you've been stuck under a rock for the last five years you'll undoubtedly have noticed that XML is starting to become part and parcel in the software industry. In the past CSV, INI and fixed width files were the standard way to exchange information between systems.

Over the last several years with the rise in cross platform development and the need to exchange information between many dissimilar systems, the need for a better way to exchange information became evident. Building on the success of HTML and the richness of the SGML document standard developed by IBM, XML was the next step. Even though XML is a simpler version of SGML, it is still pretty complex. With the introduction of MX, ColdFusion now natively supports working with XML documents. Ten years ago ColdFusion made web application development easier and faster in the same way ColdFusion makes it possible to work with XML without being overwhelmed by its complexities.

To demonstrate the power of using ColdFusion to access XML data, we'll look at a small sample application that allows you to search through an iTunes library. iTunes stores information about your music files in an XML file. We'll look at an application that allows people to search for songs in your iTunes library through a web interface using ColdFusion.

XML Overview
An XML document at its most basic level is a collection of information stored in a structured tag-based format. Unlike HTML this format is not rigidly defined by a set of standardized tags. In XML you can come up with your own tags to describe your data. You also have the option to generate a schema that describes how different elements in the XML file fit together. There are two ways to describe a schema: Document Type Definition (DTD) and XML Schema Definition (XSD). DTD was part of the XML standard from the very beginning, but XSD is much more powerful. With either schema you can validate an XML file to confirm that it contains valid data. This can be very useful when receiving files from third parties that have to conform to a certain standard. Another powerful feature of XML documents is the ability to create an eXtensible Stylesheet Language (XSL) document. Based on such a document an existing XML file can be transformed into a different format.

XML in ColdFusion
ColdFusion supports the creation, parsing, transforming and searching of XML files through a small set of powerful functions. Like all other ColdFusion Functions, the XML functions are fully internationalized, allowing you to write XML containing information in different languages and character sets. If you have ever written JavaScript, you might be familiar with the Document Object Model (DOM). This hierarchical structure of nodes is how your browser stores a web page in memory. XML documents are also represented in a DOM-like fashion. ColdFusion represents each XML node as a structure with a number of attributes like XmlName, XmlAttributes and XmlChildren. With ColdFusion access to information in an XML document is as easy as accessing information in a structure.

Accessing XML Data in ColdFusion
To start we need to understand how iTunes stores the information about your music files. If you have iTunes installed, you can search your hard drive for the "iTunes Music Library.xml" file. If you open this file in a text editor, you'll see the information about your music library stored in an XML format. ColdFusion MX was the first version of ColdFusion to include support for working with XML files. CFMX7 built upon this to add even more features for XML transforming, validating and searching. By calling the xmlparse() function and passing in a string, file location, or URL, you can create an XML document object. To open the iTunes XML file, we call:

<cfset variables.xmlDoc = XmlParse(getProperty('iTunesLibrary'))/>

If the file is valid ColdFusion will create an XML Document object out of the XML data. To use this information, we have to understand a little about how iTunes stores the data. Here are the first three lines of the 'iTunes Music Library.xml' file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">

The first line tells your XML parser that the information that is to follow adheres to the XML standard 1.0 and that the data is UTF-8 encoded. The next line describes the document type, and in this case, also gives a DTD file that explains the schema for the XML data that is to follow. If you follow the link to the DTD file, you'll notice that it is very generic. Apple Computer uses the Property List format for much of its XML files and keeps the format very basic. The next line is the Document Element, or Root node of the XML document. All the XML defined in this document must fall between the starting and closing Document Element Tags. Between this <plist> tag we find a series of <dict> tags which enclose a succession of <key> tags followed by an <integer>,<string> and other <dict> tags. The full track listing is enclosed in the first <dict> tag that follows the <key> tag with the value 'Tracks'. This happens to be the first <dict> tag that is nested in the root <dict>. Because this is really the only part in the XML file we're interested in we'll create a variable to hold that sub-section

<cfset tracks = xmlDoc.plist.dict[1].dict[1]/>

The track data is organized as a series of <key> tags holding a unique (to this library) track ID following the <key> tag is another <dict> tag which holds a series of <key> tags like Name, Artist and Album. followed by <string> and <integer> tags holding the values for these <key> tags. This format is not very easy to eyeball, so it is possible to use an eXtensible Stylesheet Language (XSL) document to translate this schema to be more intuitive. XML.com has an excellent example of an XSL to do this conversion at www.xml.com/pub/a/2004/11/03/itunes.html?page=last.


Page 1 of 2   next page »

About Leon Oosterwijk
Leon Oosterwijk is a senior Web developer for the Lampo Group (www.daveramsey.com). He lives in Nashville, TN, and his first exposure to ColdFusion occurred in 1996.

CFDJ LATEST STORIES . . .
3rd International Virtualization Conference & Expo: Themes & Topics
From Application Virtualization to Xen, a round-up of the virtualization themes & topics being discussed in NYC June 23-24, 2008 by the world-class speaker faculty at the 3rd International Virtualization Conference & Expo being held by SYS-CON Events in The Roosevelt Hotel, in midtown
AJAX World – Personal Branding Checklist
This is a checklist of items you need for an all-encompassing personal branding strategy. Personal branding is the process of marketing and selling yourself as a brand in order to gain success in business. Personal branding is a continual process just as knowing yourself is a continual
What Is ColdFusion in the Age of Java?
As CFML developers start to learn Java and move into the realm of Spring and Hibernate, it is very important to stop and ask 'What Is ColdFusion?'. ColdFusion, since CFMX, has been a J2EE application running within a J2EE server (JRun, JBoss, Tomcat, Websphere, etc.). This is important
Opinion: Give ColdFusion Some Room to Breathe
My personal approach has become to to let ColdFusion do what it does best, and no more. No AJAX generation or any of that silly UI stuff. Leave that to the AJAX frameworks, or Flex, or whatever your UI is going to be on the front-end. That's what the UI tool was designed for, CF wasn't
Viewpoint: Not Every ColdFusion Developer Should Be A Flex Developer
I am going to go ahead and contend that although a good number of ColdFusion developers can grasp and understand Flex very well, there are also a good number of ColdFusion developers who have no business going anywhere near Flex. Why do I say this? I am a big fan of Flex. I use it dail
JavaOne 2008: Sun Talks Up its Late-to-the-Party AIR-Silverlight Rival
At Java One this week Sun has been selling its year -old-but-still-upcoming - and definitely late-to-the-party - Adobe AIR- and Microsoft Silverlight-competitive JavaFX Rich Client environment as a potential revenue-generator capable of putting ads on mobile applications and JavaFX Scri
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON FEATURED WHITEPAPERS

ADS BY GOOGLE