To further ease this process, Flash MX Professional 2004 introduced the XMLConnector as a component. Using this component, it becomes trivial to consume an XML document, and display selected parts, with a minimum of coding. For those who prefer a more code-centric approach, there is an XML class that can be used instead. Figure 1 shows an XML connector being used to read a books.xml file and binding the book titles from that file to a combo box.
 | |
| Figure 1. The XML Connector: The XMLConnector greatly eases the process of working with XML files. The Properties panel at the bottom of the screen shows the parameters set to read the XML file, while the Component Inspector panel shows the resulting data being bound to the combo box. |
In the same way that LoadVars could be used to read a static or dynamic text file, you can also use XML for both static and dynamic XML files. ColdFusion gives several options for creating XML files. The next two code samples show the ColdFusion page used to create an XML file and the resulting XML file, respectively.
The XML file creation script here is very similar to the text file creation script shown in earlier. The key difference is that with XML it is much easier to associate related data, allowing for easier access to other information about the files such as date last modified and file size.
<cfdirectory
directory="c:\cfusionmx\wwwroot\devx"
action="list"
name="fileList">
<files>
<cfloop query="fileList">
<cfoutput><file name="#fileList.name#" size="#fileList.size#"
modified="#fileList.dateLastModified#"/></cfoutput>
</cfloop>
</files>
With a ColdFusion page in place to dynamically create the XML file, you can easily use this data in a Flash application by simply telling Flash to load the .cfm page as XML. The ActionScript for making use of this XML file is below.
The movie consists of a Combo box and two text fields. When the XML object is done loading the XML document supplied by ColdFusion, the combo box is populated with the name attribute as the label and the entire file object as the data. With this underlying data, the size and date modified properties are available to populate the text fields when the selected item is changed.
var cbFile:mx.controls.ComboBox;
var txDate:TextField;
var txSize:TextField;
var xFile:XML = new XML();
function handleEvent(evtObj:Object){
if(evtObj.type == "change"){
txDate.text = cbFile.selectedItem.data.attributes.modified;
txSize.text = cbFile.selectedItem.data.attributes.size + " bytes";
}
}
function loadCombo(){
for(i=0; i<xFile.firstChild.childNodes.length; i++ ){
cbFile.addItem(
xFile.firstChild.childNodes[i].attributes.name,
xFile.firstChild.childNodes[i]);
}
}
xFile.ignoreWhite = true;
xFile.load("fileList.cfm");
xFile.onLoad = loadCombo;
cbFile.addEventListener("change", this);