The forums have permanently moved to This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

Results 1 to 7 of 7

Thread: importing Boolean and other data types from XML

  1. #1

    importing 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
    <VAR fgFillEnable="false"/>
    <VAR FGFILLCOLOR ="0xAFC462" />     
    <VAR FGDFILLOPACITY="0.2" />        
    Variables declared in my script:

    var fgFillEnable:Boolean;
    var fgFillColor:int;
    var fgFillOpacity:Number;
    and loading from xml:

    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(;
    	fgFillEnable = myXML.VAR.@fgFillEnable;
    	fgFillColor = myXML.VAR.@FGFILLCOLOR;
    	fgFillOpacity = myXML.VAR.@FGDFILLOPACITY;

  2. #2
    you can cast those strings:
    // 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");
    // but look what this does - i.e if there is a mistake
    fgFillEnable = Boolean("falssssssssssssse");
    fgFillColor = uint("0x123456");
    fgFillOpacity = Number("0.2abbbbbbb");
    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.

  3. #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
    var s:String = '0';
    var b:Boolean = Boolean(int(s));//cast to int then boolean as Boolean('0') is true
    might not be useful but thought it was kinda on topic

  4. #4
    Thanks Shaedo & creatify. I'll have to take a look at this in the morning.

    Cheers for now!

  5. #5
    @ 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
    ....and I always force attributes and text nodes to a string using toString() so you are casting on and XML or XMLList object
    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....
    Last edited by orionrush; July 23rd, 2010 at 11:07 AM.

  6. #6
    Registered User
    I don't know if this is the most efficient way to do this, but what I usually do is the following:

    var newBoolean:Boolean = Boolean(node_name == "true");
    ...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.

  7. #7
    Nice one milkmit - the solution that finally worked for me follows:

    booleanTest_false = (myXML.VAR.@booleanTest_false.toString() === "true") ? true : false;
    The above of course sets a bias to false, while following would set the bias to true:
    booleanTest_false = (myXML.VAR.@booleanTest_false.toString() === "false")? false:true;
    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().

    @creatify thanks for that!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Home About Meet the Moderators Advertise

 Link to Us


Copyright 1999 - 2012