In Flash, to add ComboBox Component and populate its labels and values from an external XML document, follow these steps. In the following example, we will load countries list and access country name using its attribute.
Create a new Flash (ActionScript 3.0) document. Drag a ComboBox to the Stage and give it an instance name of myCB . On the Parameters tab.
Let’s define some sample xml structure and data like this:
<?xml version="1.0" encoding="utf-8"?> <countries> <country name="USA"> </country> <country name="UK"> </country> <country name="Australia"> </country> <country name="NZ"> </country> <country name="Canada"> </country> <country name="Pakistan"> </country> </countries>
Open the Actions panel, select Frame 1 in the main Timeline, and enter the following code:
import fl.data.DataProvider; var country:String=''; var countriesList:Array = []; //URLLoader class helps you to load data from an external source such as a URL var xmlLoader:URLLoader = new URLLoader(); var xmlData:XML = new XML(); xmlLoader.addEventListener(Event.COMPLETE, LoadXML); xmlLoader.load(new URLRequest("countries.xml")); function LoadXML(e:Event):void { xmlData = new XML(e.target.data); xmlData.ignoreWhite = true; ParseCountries(xmlData); //trace(xmlData); } function ParseCountries(countriesInput:XML):void { var countriesListing:XMLList = countriesInput.country.attributes(); var i:int; for each (var countryName:XML in countriesListing) { //populate the countriesList array to be used with ComboBox countriesList.push( {label:countryName, data:countryName} ); trace(countryName); } myCB.prompt = "Select Country:"; myCB.dataProvider = new DataProvider(countriesList); myCB.addEventListener(Event.CHANGE, dropDownHandler); myCB.dropdown.rowHeight = 30; myCB.width = 150; } function dropDownHandler(event:Event):void { country = ComboBox(event.target).selectedItem.data; trace("selected country: "+ country); //do something here whatever you want }
Hope that helps.
Cheers!
Good Afternoon,
Thank you so much for your post. I am trying to do the samething but i couldn’t populate the combobox with your code.
I would like to create a combobox that will display the names from an external xml file and when you select a name, multiple text field will be populated with the data that the external xml files has.
I am able to load the combobox with the following code but I am not able to populate the multiple text field.
myCB.prompt = “Select Name”;
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
var xmlAry:Array = new Array();
var xmlURL:Array = new Array();
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest(“roster.xml”));
function LoadXML(e:Event):void {
xmlData = new XML(e.target.data);
trace(xmlData..name.length());
for(var i:uint=0;i<xmlData..name.length();i++)
{
xmlAry.push(xmlData..name[i]);
xmlURL.push(xmlData..url[i]);
myCB.addItem( { label:xmlAry[i], data:i } );
myCB.addEventListener(Event.CHANGE, action);
}
}
function action (e:Event):void {
var no:Number = Number(myCB.selectedItem.data);
trace(xmlURL[no]);
}
First get the data populated in the drop down correctly and then you can move ahead from there to fill the data in other fields. You will have to target other fields wih the required data.
Thank you very much…