XML and arrays question

Good morning!

I am elementary school teacher and I made a little app that allows a teacher to click a button and randomly select one student’s name from a XML imported array. The selected name is then removed from the list and when the teacher clicks the button again it gives them a new name and so on all the way through the class list. When the teacher reaches the end of the names, the list is rescrambled and the app starts over. The app. only imports one class list.

The teachers love it and it works great, but they would like an upgrade–they would like to be able to click a combobox and be able to select from a list of class lists to load different sets of student names. And this is where I am stuck. I have been trying to use the XMLconnector component and have been able to populate the combobox with the names of the classes, however I am not sure how to proceed from there.

So…here is my questions: should I continue in this direction using the XMLconnector? If not, what would you suggest? How does the XMLconnector access the XML array that was loaded and what would it be called? Is it better to try and write it by hand–you will see in my AS that I was playing with the idea of writing it by hand.

Here is my simple XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<teaching>
<myClass name="Kindergarten">
	<student>Davey Pliska</student>    	
	<student>Sarah L.</student>
	<student>Madeline Pliska</student>
	</myClass>
 <myClass name="First">
	<student>Su</student>
	<student>Homer</student>
	<student>Huey</student>
	<student>Kim</student>
	<student>SpongeBob</student>
	<student>J.D.</student>
</myClass>
</teaching>

Here is my AS:

stop();
//
// Trigger Data Source Behavior
// Macromedia 2003
this.myConnection.trigger();
///////////////////
myName.text = "?";
myStudentList = new Array();
myClassList = new Array();
//////////
/*------- I was commenting this out because it is not needed by the connector and I was fishing.
function loadXML(loaded) {
	if (loaded) {
		xmlNode = this.firstChild;
		myStudents = [];
		total = xmlNode.childNodes.length;
		for (i=0; i<total; i++) {
			myStudents* = xmlNode.childNodes*.firstChild.nodeValue;
		}
		myClasses = new []();
		myClasstotal = xmlNode.childNodes.length;
		for (z=0; z<myClasstotal; z++) {
			myClasses = xmlNode.childNodes[z].nodeValue;
		}
		trace(myClasses);

		myStudentList = myStudents.concat();
		// Shuffle word array
		shuffles = Math.floor(Math.random()*(99));
		myStudents.shuffle(shuffles);
		myName.text = "Ready";
	} else {
		content = "file not loaded!";
	}
}
--------------------------------------*/
///////// Array shuffle function
Array.prototype.shuffle = function(times) {
	var max = this.length;
	var a, b, temp;
	while (times--) {
		a = Math.floor(Math.random()*max);
		b = Math.floor(Math.random()*max);
		temp = this[a];
		this[a] = this**;
		this** = temp;
	}
};
///////buttons///////////////////
pickMe_btn.onRelease = function() {
	theName = myStudents[0];
	myName.text = theName;
	trace(theName);
	myStudents.shift();
	if (myStudents[0] == undefined) {
		myName.text = "Done";
	}
};
reset_btn.onRelease = function() {
	myStudents = myStudentList.concat();
	shuffles1 = Math.floor(Math.random()*(99));
	myStudents.shuffle(shuffles1);
	myName.text = "Ready";
};
/*//////XML////////////---commented out not needed by the connector
xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("myStudents.xml");
//////////////////////////////////*/

And [COLOR=“Red”]attached is my fla[/COLOR]–sorry too large! and xml and a zip to the first version.

Any ideas or help would be greatly appreciated!

Thanks,

Madzander

Pier25,

Thanks for the idea of using XMLObject. I am using XML2Object from sephiroth-- an updated version–I think. I already have the data being read out of the xml and into the arrays I need to randomize. I really need someone to point me in the right direction and you did just that! I will a link to the finished project soon.

Thanks again,

madZander

I finished the project and it seems to work pretty well. Special thanks to Pier25 and Sephiroth for XML2Object. Here is how I did it:

I used two buttons, a combobox, and a dynamic textbox.

/**  It's My Turn ::  madzander copyright 2008.
This is a widget for teachers to use in their classroom to randomly pick students from multiple groups.
Special thanks to Sephiroth at http://www.sephiroth.it/ for the XML2Object and 
Charlie at BrainGiants http://www.BrainGiants.com for the Array Shuffle Function
*/
import it.sephiroth.XML2Object

// xml file for the test
xmlFile = "./myStudents.xml"
///vars
myNewClass = new Array();
myReloadClass = new Array();
menuArray = new Array();
var selectedClass
var pm = 0;
myName.text = "Select a class";

// now load the XML using the classic
// XML class
myXML = new XML();
myXML.load(xmlFile);
myXML.onLoad = function(){
	trace("XML loaded");
	// now initialize the XML2Object class
	var xml_loader:XML2Object = new XML2Object();
	// get the native flash object using the parseXML method, and
	// passing the XML itself as argument
	as2obj = xml_loader.parseXML(this);
	
	//create array of menu items
	mynumberClasses = as2obj.teaching.myClass.length;
	for (i = 0; i < mynumberClasses; i++) {
		menuArray* = as2obj.teaching.myClass*.attributes.name;
	}
	//set dataprovider for menu
	myComboBox_cb.setDataProvider(menuArray);
}

//make array with selected names
makeClass = function(b) {
	total = as2obj.teaching.myClass**.student.length
	for (i=0; i<total; i++) {
		myNewClass* = as2obj.teaching.myClass**.student*.data;
	}
	//make reload array
	 myReloadClass = myNewClass.concat();
	//shuffle name array
	// Shuffle word array
	shuffles = Math.floor(Math.random()*(99));
	myNewClass.shuffle(shuffles);
}
///////// Array shuffle function from BrainGiants!  Thanks, Charlie
Array.prototype.shuffle = function(times) {
 var max = this.length;
 var a, b, temp;
 while (times--) {
  a = Math.floor(Math.random()*max);
  b = Math.floor(Math.random()*max);
  temp = this[a];
  this[a] = this**;
  this** = temp;
 }
};
///////buttons///////////////////
pickMe_btn.onRelease = function() {
	theName = myNewClass[0];
	myName.text = theName;
	myNewClass.shift();
	pm += 1;
	if (pm -1 >= myReloadClass.length) {
		myName.text = "Done";
	}
}
reset_btn.onRelease = function() {
	myNewClass = myReloadClass.concat();
	shuffles1 = Math.floor(Math.random()*(99));
		myNewClass.shuffle(shuffles1);
		myName.text = "Ready to go again?";
		pm = 0;
}

and on the combobox:


on (change) {
	selectedClass = this.getSelectedIndex();
	_root.makeClass(selectedClass);
	_root.myName.text = "Ready";
	_root.pm = 0;
}

Thanks for your help!

David

Congratulations :wink: