Tracing XMLList within a switch statement returns null
Just found really nasty bug in Flash; When using XML Filtering within a switch statement it will fail when assigning that value to an XMLList variable and trace null. (Even though there is data)
When you declare the variable outside/above of the switch it does work. What can also be done is assigning the variable to a function that does the filtering.
Apparently you cant assign and set a variable on the same line within a switch because of a scoping issue. Also checked JIRA, Adobe’s BUG database, and it is already filed
Here are some examples:
//FAILS!
switch(someName) {
case "myName":
trace(_xml.myItems.(@id == "1")) // traces the inside of XMLList
var items:XMLList = _xml.myItems.(@id == "1");
trace( items ); // traces null
break;
}
// This will give us a TypeError: Error #1010: A term is undefined and has no properties.
//SUCCESS!
var items:XMLList;
switch(someName) {
case "myName":
trace(_xml.myItems.(@id == "1")) // traces the XMLList
items = _xml.myItems.(@id == "1");
trace( items ); // traces the inside of XMLList
break;
}
//ALSO SUCCESS!
var items:XMLList;
switch(someName) {
case "myName":
trace(_xml.myItems.(@id == "1")) // traces the XMLList
items = getFilteredXML(_xml.myItems, "id", "1");
trace( items ); // traces the inside of XMLList
break;
}
function getFilteredXML(pList:XMLList, pAttribute:String, pValue:String):XMLList {
return pList.(attribute(pAttribute) == pValue);
}
Hope this saves somebody else a couple of hours bughunting …






It did. Thank you very much!
Where is You temple?
I will pray to You starting from Yesterday!
Its in the North
I was going insane over this problem! I’d already lost a few hours, but this saved me from wasting a few more. Cheers
I really enjoyed reading this post, big fan. Keep up the good work and
please tell me when can you publish more articles or where can I read
more on the subject?
Thank’s for this post! It just saved me a lot of time!
My organization is very enjoyed in this side. Its a sexy topic. It aid me very much to eliminate some problems. Its opportunity are incredibly fantastic and dealing style so speedy. It could very well be help all of you. Thanks.