Monday, May 19, 2008

Odd behavior with E4X filtering in AS3

This one drove me nuts today and so I thought I'd better blog it. I was trying to create a function that to filter an xml file using E4X and locate a node with a specific "photoid" attribute:

First I tried to get retrieve the node with the correct id by filtering:

var myList:XMLList = pdXML..photo.(@photoid=="123");

but, it appears that unless the XMLList has more than one item it won't trace anything out when you do this:

trace(myList.toString()); //shows nothing
trace(myList); //also shows nothing


...even though the list clearly has one item in it:

trace(myList.length()); //returns 1

The only way that it will show the node attribute value is when you refer specifically to the attribute in the trace statement like this:

trace(myList.@photoid); // shows "123"

If the xmlList has more than one node it traces fine using toString(). But the thing that JUST occurred to me is that I SHOULD have used toXMLString()-- which (as I just correctly tested) traces the single node correctly. Even so, the xmlList should trace properly even without using the toString() or toXMLString() methods -- like this: trace(myList);

In any event, I'm not sure if this is a bug or a feature but it sure was a frustrating way to spend an afternoon...

No comments: