R – Why, in AS3, are XML and XMList unrelated (inheritence wise) classes

actionscript-3e4xxml

I can't help think that making XML ad XMLList both unrelated, as in both extend Object directly, is a design flaw in the AS3 core library. Surely having XML extend XMLList would be a much cleaner system, where XML is considered an XMLList with only one member?

This would also avoid the very annoying practice of an E4X query possibly returning either an XML or XMLList, which can result in a casting error.

So is there any reason I'm not thinking of that XML and XMLList were designed to only have Object as a common type?

Best Answer

It seems that according to the ECMA spec for E4X (p22) and the XMLList documentation, that the XMLList class does contain the methods of the XML class, but only when there is one member. So when performing an E4X query the result should always be stored in a variable of type XMLList.

Beforehand, due to the naming of these objects, I presumed that an XML object represented a complex tree of XML element data, but according to the spec(p12), this is not true, "Each value of type XML represents an XML element, attribute, comment, processing-instruction or text node".

It is the XMLList class that should be used as the more generic type of the two: "A value of type XMLList represents an XML document, XML fragment or an arbitrary collection of XML objects (e.g., a query result)"

So this solves my casting issue, as I should have never stored E4X queries as a variable of type XML. I should only use the XML class for iterating over XMLLists and breaking apart XML elements.