Similar to my last post on parsing a Date object, you can’t assign XML data directly to a Boolean object or the value will always end up being ‘true’. This is because the Boolean() constructor parses all non-null XML data as true.
Consider the following XML:
var response : XML = <response> <status valid="false">200</status> </response>; |
To get the data in the ‘valid’ attribute and assign it to a Boolean object, you’d do the following in your code:
var validString : String = response.status.@valid; var valid : Boolean = (validString == "true"); |
Note that this will only work if the validString is all lowercase, so you may want to call toLowerCase() on validString just to be safe.
3 comments
Comments feed for this article
August 4, 2011 at 10:00 pm
Scott Enders
Or you can do this:
var valid : Boolean = true.toString();
I like this better because “true” could accidentally be misspelled.
August 9, 2011 at 6:54 am
Tahire Khan (@tahirekhan)
Great tip, can you recommend an xml editor as well, I am looking at Liquid Editor (http://www.liquid-technologies.com/xml-editor.aspx) because we have it at work, any thoughts?
August 9, 2011 at 5:51 pm
Nick Schneble
I usually edit XML within Eclipse, since that’s what I use for Flex and Flash development. For quick edits, I sometimes use TextWrangler. I don’t have any experience with Liquid Editor, but if it does what you need, I’d stick with that.