Results 1 to 7 of 7
-
July 22nd, 2010, 03:01 PM #126Registered User
postsimporting Boolean and other data types from XML
Hi all,
apologies for any cross posting.
Im trying to wrap my head around data types and phrasing them from xml. From what I gather variables that come in from XML are always treated as a string - even if you declare them as something else before hand?
Im not seeing that myself as everything else other then the Boolean is behaving as expected. For example in the code below, if I declare FGFILLCOLOR as anything other then an int, or Number, I get an unrelated type error.
So why just Boolean and how can I convert them so they behave properly without declaring all of them as a String?
Though I've seen a few related solutions posted around but I wan't able to sort them out.
thanks for your time all.
Here is what I've got:
the xml
Variables declared in my script:Code:<VARIABLES> <VAR fgFillEnable="false"/> <VAR FGFILLCOLOR ="0xAFC462" /> <VAR FGDFILLOPACITY="0.2" /> </ VARIABLES>
and loading from xml:Code:var fgFillEnable:Boolean; var fgFillColor:int; var fgFillOpacity:Number;
Code:var myXML:XML; var varloader:URLLoader = new URLLoader(); varloader.load(new URLRequest("variables.xml")); varloader.addEventListener(Event.COMPLETE, onLoaded); function onLoaded(e:Event):void { myXML = new XML(e.target.data); fgFillEnable = myXML.VAR.@fgFillEnable; fgFillColor = myXML.VAR.@FGFILLCOLOR; fgFillOpacity = myXML.VAR.@FGDFILLOPACITY; }
-
July 22nd, 2010, 03:36 PM #2
you can cast those strings:
You may also look into the as operator and the is operator - it actually might be best to use as, I'm kinda rushing this post. If there is an error in your data, you can use the is operator to check if the cast data value is Boolean for example.Code:// technically, a color is a uint and I always force attributes and text nodes to a string using toString() so you are casting on and XML or XMLList object fgFillEnable = Boolean(myXML.VAR.@fgFillEnable.toString()); fgFillColor = uint(myXML.VAR.@FGFILLCOLOR.toString()); fgFillOpacity = Number(myXML.VAR.@FGDFILLOPACITY.toString()); // so if you run this, it should work fgFillEnable = Boolean("false"); fgFillColor = uint("0x123456"); fgFillOpacity = Number("0.2"); trace(fgFillEnable) trace(fgFillColor) trace(fgFillOpacity) // but look what this does - i.e if there is a mistake fgFillEnable = Boolean("falssssssssssssse"); fgFillColor = uint("0x123456"); fgFillOpacity = Number("0.2abbbbbbb"); trace(fgFillEnable) trace(fgFillColor) trace(fgFillOpacity)
-
July 22nd, 2010, 03:49 PM #3
To make things that little bit smaller (useful if you are storing a LOT of boolean values in xml)
keep in mind that typing 0 to boolean is false (note all other values are true as <0 AND >0 are bother true)
so rather than storing the string 'false' you can store just the string '0' and then
might not be useful but thought it was kinda on topicCode:var s:String = '0'; var b:Boolean = Boolean(int(s));//cast to int then boolean as Boolean('0') is true trace(b);//false
-
July 22nd, 2010, 05:16 PM #426Registered User
postsThanks Shaedo & creatify. I'll have to take a look at this in the morning.
Cheers for now!
-
July 23rd, 2010, 10:14 AM #526Registered User
posts@ creatify
regarding the use of 'is' for testing data types, is this what you mean?:
trace("booleanTest_true is Boolean : "+(booleanTest_true is Boolean) );//true
*** Edit ADD
I thought that items coming out of XML were already treated as strings. I though this was why my Boolean values were always coming up as true regardless of what was in there........and I always force attributes and text nodes to a string using toString() so you are casting on and XML or XMLList objectLast edited by orionrush; July 23rd, 2010 at 11:07 AM.
-
July 23rd, 2010, 11:06 AM #6497Registered User
postsI don't know if this is the most efficient way to do this, but what I usually do is the following:
...which basically sets newBoolean to 'true' if the value of the node is "true", and to 'false' if it's 1) anything else, or 2) non-existent.Code:var newBoolean:Boolean = Boolean(node_name == "true");
-
July 23rd, 2010, 11:51 AM #726Registered User
postsNice one milkmit - the solution that finally worked for me follows:
The above of course sets a bias to false, while following would set the bias to true:Code:booleanTest_false = (myXML.VAR.@booleanTest_false.toString() === "true") ? true : false;
I was banging my head agains the data type until I really started thinking about what creatify has said about forcing it to a string with .toString().Code:booleanTest_false = (myXML.VAR.@booleanTest_false.toString() === "false")? false:true;
@creatify thanks for that!

Reply With Quote

Bookmarks