views:

141

answers:

2

I've got XML that looks like this:

<item>
     <itemDate>07/10/2009</itemDate>
</item>

I would like to be able to read this in as an E4X object: item.itemDate and have itemDate be an ActionScript Date object instead of a string. Is this possible?

+4  A: 

If I understand you right, Nope. Everything that comes in as XML will be a string - even other primitives like "1", or "false".

When I have to deal with something like this, I iterate through the XML and create a mirroring Object (sometimes an untyped Object, even) and convert the String values to their appropriate datatypes.

Or, I just convert the value to its appropriate datatype just before use.

Ross Henderson
+3  A: 

Just pass the value to the Date constructor:

for each(var itemNode:XML in doc.item)
{
    var itemDate : Date = new Date(itemNode.itemDate);
}
Richard Szalay
Will that work? Really? The itemDate object there isn't truly a String; it's an XML object Text node. Is that going to work?
Chris R
Try it and see. The AVM does a substantial amount of implicit coercions. If it doesn't work, just change it to itemNode.itemDate.toString()
Richard Szalay
+1... was just abut to answer it myself!
James Hay