Booleans in XML Gotcha
The following code highlights a small but easy to miss gotcha relating to XML and Boolean types. The trace output will indicate that the flag is true. This is because when a Boolean is inputted into an XML object it is cast into the string “true” or the string “false”, but when it is parsed out it casts the string to Boolean, returning true if the string is non-null.
var flag:Boolean;
var xml:XML;
flag = false;
xml = <xml value={flag} />;
flag = xml.@value;
trace(xml.toXMLString());
trace(flag);
The result is that serializing a Boolean into XML and deserializing it from XML will break. Happily, the solution is simple, to retrieve the value
flag = xml.@value == "true";
will guarantee that the Boolean is cast correctly.



Pingback: vizio 360º » Parsing XML: good practice
Pingback: XML Gotcha – new XML(null) at AlecMcE.com