Skip to main content
Inspiring
June 25, 2008
Question

How to populate list component via xml file?

  • June 25, 2008
  • 1 reply
  • 288 views
There is a TextArea component that should show the name and the description of the item selected in the list component. But I dont know how to populate list with external XML and what should be the coding in flash as well as what should be written in the XML. Please help.
This topic has been closed for replies.

1 reply

Inspiring
June 25, 2008
Here's an xml file listing a couple of brother comedy teams:
<?xml version="1.0" encoding="UTF-8"?>
<team>
<brothers>
<Marx>
<name>Groucho</name>
<name>Chico</name>
<name>Harpo</name>
<name>Zeppo</name>
<name>Gummo</name>
</Marx>
<Howard>
<name>Moe</name>
<name>Curly</name>
<name>Shemp</name>
</Howard>
</brothers>
</team>

Open a new .fla and save it in the same folder as the .xml file. Place a List Component on the Stage and name it (in this case, "comicTeams_list"). In the first frame write the following ActionScript:

//create XML object and load external xml file
var broList:XML = new XML();
broList.ignoreWhite = true;
broList.onLoad = processList; // this is a function that will be written below
broList.load("populateList.xml");




function processList(success:Boolean):Void{
if(success){
loadList();
}else{
trace("Load failure");
}
}

function loadList():Void{
var broName:String;
var listEntries = broList.firstChild.childNodes[0].childNodes[0].childNodes.length;
for(var i:Number = 0;i<listEntries;i++){
broName = broList.firstChild.childNodes[0].childNodes[0].childNodes .childNodes[0].nodeValue;
trace(broName);
comicTeams_list.addItem(broName);
}
}


//to make something happen when you click on a name in the List, create a Listener and function
var broListListener:Object = new Object();
broListListener.change = someAction; //"someAction" is a function to be written shortly
//add the Listener to the List
comicTeams_list.addEventListener("change", broListListener);

function someAction(evtObj:Object):Void{
var pickedBrother:String = evtObj.target.selectedItem.label;
//write actions here, referencing pickedBrother variable
}


The names of the Marx Brothers will appear in the box.

This is written in AS2. When you post a question, it's a good idea include which version of ActionScript you're using.