giovedì, giugno 19, 2008

XML Attribute: Do You Exist?

I was just wresting with how to deal with this conundrum. I found a posting by Senocular on UltraShock that explains it beautifully. You know, IMO Senocular has single-handedly raised the level of ActionScript help by leaps and bounds.

Q: is it possible to check if a XML node attribute actually exists when using the XML class in AS3? From what I can work out, there doesn't seem to be a way to tell if an attribute is empty (i.e. exists with no value.. an empty string) or if it is non-existent, in both cases a XMLList object is always returned containing an empty string as it's value.

A: when you get an attribute via @ property or attribute(), you're actually getting an XMLList. As a list, you can check for the existence of attributes by using length().

var xml:XML = <xml att="" />;
trace(xml.@att.length()); //
trace(xml.@foo.length()); // 0