Dynamic variable to parse in a XML

A current problem in AS3 with XML : if you want to parse a XML with a dynamic variable as a root, you can’t with a for each statement. It doesn’t recognize the variable.

The XML Format :

<?xml version="1.0" encoding="UTF-8"?>
	<Box name="Box1">
		<lien nom="BENCHMARK">http://www.google.fr</lien>
		<lien nom="FOCUS GROUP">http://www.google.fr</lien>
	<Box name="Box2">
		<lien nom="FORMATION">http://www.google.fr</lien>
		<lien nom="COACHING">http://www.google.fr</lien>

The way which doesn’t work (N.B. in this case the XML is Box1…/Box1 Box2…/Box2:

var myBox:String = "Box1";
for each (var box:XML in myXML.myBox) {

The good way :

private function _xmlLoaded(mEvt:Event):void {
	mEvt.target.removeEventListener(Event.COMPLETE, _xmlLoaded);
	_xml = new XML(mEvt.target.data);
        _boiteNom = "Box1";
	_textes = [];
	_liens = [];
	var indexBoite:uint = _getIndexBox(_boiteNom);
	_nbrFiches = _xml.Box[indexBoite].lien.length();
	for (var i:uint; i < _nbrFiches; ++i) {
private function _getIndexBox($name:String):uint {
  	for(var i:uint; i < _xml.Box.length(); ++i) {
   		if(String(_xml.Box[i].@name) == $name)
   			return i; 
   return 666; // The devil's number, too evil to be real

Leave a Reply

Your email address will not be published. Required fields are marked *