Actionscript 3 XML Weirdness

Weird. Have just been working on a project in AS3, and have been scratching my head over a blank trace from what (as far as I could see) should be a simple XML lookup.

[kml_flashembed movie="/blog/swf/xml_trace.swf" height="350" width="300" version="9" /]
Direct link to SWF

An example of the XML im using :

<pictures>
  <img src="example.jpg" />
</pictures>

The ActionScript :

function BuildImageList(vXML:XML):void{
  trace(vXML.pictures.img);
}

As far as I was aware, this should output <img src=”example.jpg” />, but it doesn’t – it just outputs blank. However, if I change the XML file and add a second <img /> node :

<pictures>
  <img src="example.jpg" />
  <img src="example.jpg" />
</pictures>

… it works :| . As expected, it traces 2 <img /> nodes. Is this a bug, or am I missing something?

  • http://blog.kirupa.com Kirupa

    Hey dan!
    It isn’t a bug at all….it’s a feature! In AS3, a collection of data such as your two img nodes is treated as an XMLList, but a single chunk of data such as your single img node is treated as an XML object.

    You can get more info on it here:
    livedocs.adobe.com/flex/2/langref/XMLList.html

    and

    kirupa.com/forum/showpost.php?p=1936562&postcount=167

    This does seem a bit more complex than the lowly XML functionality found in AS2 :P

    Cheers!
    Kirupa

  • dan

    Good to know, thanks Kirupa :)

    I’ll have a read of your post, to clarify.

  • http://blog.kirupa.com Kirupa

    Oh, it looks like I forgot to fully answer your post. To see the missing value, you’ll have to explicitly add a toXMLString as in:

    trace(vXML.pictures.img.toXMLString());

    =)

  • http://shiftperception.com dan

    Cool, cheers.

    Yeah I was scratching my head for a while – cause the data was still getting passed, it was just not tracing out. Crisis over :)