This is one of those things that annoys me, with ActionScript 3: the syntax for getting the length of an array is different from getting the length of a generation in XML.
When getting the number on elements in an array, you use length as a property of the array rather than a method. Such as
var max = myArray.length;
But to get this from an XML object, you have to add function brackets like this
var kids = myXML.length();
I'm guessing this is because automatically tracking the number of elements in an array is much simpler than for an XML object....certainly requires less overhead if the XML object is very complex.
Call me lazy, but I don't want to have to remember that length is a property of an array and a method of an XML object. Some consistency in syntax would be nice, even if inside it acts like a property sometimes and method at other times. It gets challenging to remember all this, especially if I'm switching between AS and other languages a lot.
Monday, February 18, 2008
Subscribe to:
Post Comments (Atom)

8 comments:
I wondered the same thing when I noticed that. My guess was that it used a method because "length" could be a child node of the xml. Since parent.child is valid E4X syntax, it wouldn't really know if you wanted "length" as in a reference to the child node(s) or "length" as in the number of child nodes.
myXML.length() doesn't yield the same as myArray.length. No matter how many child nodes are in your XML object, the length property will be returned as 1.
Thanks for that post. I agree. This post as really helped me. I had been stuck the last 4 hours because i couldn't display the length. Now everything works fine.
Make sure you have an xmlList object instead of xml object to use the length property. Otherwise you always have 1 returned.
Totally agree. No matter the reason it´s sort of silly and not intuitive. If they needed to mark the diference they could have made it "nodesLength" or something. Having the same name just causes confusion. I wonder how many hours overall have been lost due to this.
I would suspect that it has something to do with the way the data is being returned. I would wager that is has something to do with how the XML class is implemented; using return methods for data access rather than directly letting you access the property values. This way, should something need to be changed internally in the future, previous code/documentation should still be valid (assuming the entire format does not have to be changed). Just my $0.02.
yeah. i totally agree. No matter why (of course they'd valid reasons) it's sort of annoying! expecially if it doesn't throw any error...
i've been stuck 2 hours pulling my hair without understanding why i couldn't get the lenght of my xmlList. And obviously as it happens sometimes, reading and reading the guide over and over didn't help me notice those fu***ng parenthesis... ( it's LENGTH !! why the hell it's not working... 'coz it's LENGTH() doh!! )
I'm a little late to this party but you can find the length of an XML object and have it return a value higher than one.
You will have to add ".*.length()" to your XML object name.
eg:
**************************
var $tkrData:XML;
$tkrData = XML(loader.data);
trace("number of child nodes = "+$tkrData.*.length());
**************************
in my case this returns 13.
Post a Comment