|
|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV SYS-CON.TV WEBCASTS |
TOP COLDFUSION LINKS CF Search
Searching through your iTunes Library using ColdFusion
Accessing XML data in ColdFusion
By: Leon Oosterwijk
Aug. 9, 2005 04:00 PM
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.
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
XML in ColdFusion
Accessing XML Data in ColdFusion <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"?> 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 » CFDJ LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
|||||||||||||||||||||||||||||||||||||||||||||||||||||