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"?> <boite> <Box name="Box1"> <lien nom="BENCHMARK">http://www.google.fr</lien> <lien nom="FOCUS GROUP">http://www.google.fr</lien> </Box> <Box name="Box2"> <lien nom="FORMATION">http://www.google.fr</lien> <lien nom="COACHING">http://www.google.fr</lien> </Box> </boite> |
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) { trace(box); } |
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) { _textes.push(_xml.Box[indexBoite].lien[i].@nom); _liens.push(_xml.Box[indexBoite].lien[i]); } _ajoutFiche(); } 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 } |