A Flash Developer Resource Site

Results 1 to 18 of 18

Thread: [RESOLVED] number variable with XML?

  1. #1
    Senior Member Ovaire d'Oz's Avatar
    Join Date
    May 2006
    Location
    France
    Posts
    148

    [RESOLVED] number variable with XML?

    Hi,
    I am a beginer in importing XML into flash, and I need a little help. I would like to write a number in an XML document and use this number as a variable in AactionScript. Is there a way that it can be done?
    Thanks.

  2. #2
    Senior Member
    Join Date
    Nov 2005
    Location
    dante's inferno
    Posts
    904
    put this code on the first frame of your flash file:
    code:

    var myDocument = new XML();
    myDocument.ignoreWhite = true;
    myDocument.onLoad = function(success) {
    if (success) {
    _root.myNumber = myDocument.childNodes[0].childNodes[0].firstChild.nodeValue;
    trace('my number: ' + myNumber);
    }
    }
    myDocument.load('myXMLfile.xml');
    stop();


    name your xml file: myXMLfile.xml

    the text for your xml file should be:
    code:

    <?xml version="1.0"?>
    <content>
    <myNumber>1</myNumber>
    </content>



    that should do it. the is the simplest way i could think of to show you.

    hope this helps

    IMS

  3. #3
    Senior Member Ovaire d'Oz's Avatar
    Join Date
    May 2006
    Location
    France
    Posts
    148
    Thanks, but the number seems to be a string. How can I make myNumber become a Number variable?
    In the end I wish to change the variable of a for loop with xml...
    Last edited by Ovaire d'Oz; 07-30-2007 at 09:41 AM.

  4. #4
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    You need to cast it to a Number type:

    var myNum:Number = Number("1");

    trace(myNum+1); //traces 2

  5. #5
    Senior Member Ovaire d'Oz's Avatar
    Join Date
    May 2006
    Location
    France
    Posts
    148
    This is what I actualy have:

    var myDocument = new XML();

    myDocument.ignoreWhite = true;

    myDocument.onLoad = function(success) {

    if (success) {

    var myNumber:Number = Number("1");

    _root.myNumber = myDocument.childNodes[0].childNodes[0].firstChild.nodeValue;

    trace('my number: ' + myNumber+1);

    }

    }

    myDocument.load('../xml/nsingles.xml');

    //Variable used for the loop
    var s:Number = myNumber+1;
    trace('me number: ' + myNumber);

    If I remove the _root in front of 'myNumber = myDocument.childNodes[0]...', the 1st trace return the number I have in the xml document (18) plus 1 at the end, so in the output windows the message is: my number: 181
    If i keep the _root, the 1st trace return 11.
    And in the second trace, it always says undefined.
    Last edited by Ovaire d'Oz; 07-30-2007 at 01:53 PM.

  6. #6
    Member
    Join Date
    May 2007
    Posts
    35
    "my number: " + myNumber + 1;

    This expression gets evaluated from left to right.
    So here is what's happening.

    Step One: First evaluate the first part: "my number: " + myNumber
    Step Two: Flash knows to convert myNumber, 18, to a string, "18".
    Step Three: Now we have "my number: " + "18" which becomes "my number: 18"
    Step Four: Now we take the next part together with what just happened:
    "my number: 18" + 1.
    Step Five: Again, flash knows to convert 1 to "1" and so our result is: "my number: 181".

    How do we get around this? Simple. Just add some brackets to fix the order of operations:

    "my number: " + (myNumber + 1)

  7. #7
    Senior Member Ovaire d'Oz's Avatar
    Join Date
    May 2006
    Location
    France
    Posts
    148
    Thanks, but brackets does not change the result. 18 should not be a string, I need it as a number.

  8. #8
    Senior Member Ovaire d'Oz's Avatar
    Join Date
    May 2006
    Location
    France
    Posts
    148
    Thanks, but brackets does not change the result. 18 should not be a string, I need it as a number.

    P.S I'm using Flash 8, I forgot to mention it if that changes anything...

  9. #9
    Member
    Join Date
    May 2007
    Posts
    35
    num = Number(str);
    What this does is convert whatever string is in the brackets to a number, if possible.

    var myNumber:Number = Number("1");
    _root.myNumber = myDocument.childNodes[0].childNodes[0].firstChild.nodeValue;

    Your first line assigns the number one to myNumber, the second line assigns a STRING! back to myNumber though. What you need is to replace those lines with something like this:

    var myNumber:Number = Number(myDocument.childNodes[0].childNodes[0].firstChild.nodeValue);

  10. #10
    Senior Member Ovaire d'Oz's Avatar
    Join Date
    May 2006
    Location
    France
    Posts
    148
    wooohooooo! It works!!! Thank you!

  11. #11
    Senior Member Ovaire d'Oz's Avatar
    Join Date
    May 2006
    Location
    France
    Posts
    148
    Although, I still have an undefined returned with the second trace.

  12. #12
    Member
    Join Date
    May 2007
    Posts
    35
    You get undefined because myNumber has the scope of the function onLoad. This means that as soon as the onLoad function is finished running the variable is no longer accessible. Add: "var myNumber:Number;" to the very top near "var myDocument = new XML();" and remove "var myNumber:Number = Number(..." from where you currently have it and replace it with just "myNumber = Number(...".

    This is because you want to make sure you're using the variable of the larger scope as opposed to a new variable, of the same name, with a more local, function scope.

  13. #13
    Senior Member Ovaire d'Oz's Avatar
    Join Date
    May 2006
    Location
    France
    Posts
    148
    Sorry my english is not perfect and I don't understand your last sentence. I still get undefined.

    var myDocument = new XML();
    var myNumber:Number;
    myDocument.ignoreWhite = true;
    myDocument.onLoad = function(success) {
    if (success) {
    myNumber = Number(myDocument.childNodes[0].childNodes[0].firstChild.nodeValue);
    trace("my number: "+(myNumber+1));
    }
    };
    myDocument.load('../xml/nsingles.xml');

    trace('me number: '+myNumber);

  14. #14
    Member
    Join Date
    May 2007
    Posts
    35
    Just because you call the load function doesn't mean the XML is already loaded at that time - in fact it most likely isn't. You are calling that trace BEFORE the XML gets loaded. Try this instead:

    var myDocument = new XML();
    var myNumber:Number;
    myDocument.ignoreWhite = true;
    myDocument.onLoad = function(success) {

    if (success) {

    myNumber = Number(myDocument.childNodes[0].childNodes[0].firstChild.nodeValue);
    trace("my number: "+(myNumber+1));
    doAfterXMLLoaded();
    }
    };
    myDocument.load('../xml/nsingles.xml');

    function doAfterXMLLoaded()
    {
    trace('me number: '+myNumber);
    }

  15. #15
    Senior Member Ovaire d'Oz's Avatar
    Join Date
    May 2006
    Location
    France
    Posts
    148
    But then, since I wish to use the myNumber variable for a for loop, have I got to put my loop in the doAfterXMLLoaded() function?

  16. #16
    Senior Member Ovaire d'Oz's Avatar
    Join Date
    May 2006
    Location
    France
    Posts
    148
    ok! It's working perfectly.
    Thanks a lot.

  17. #17
    Member
    Join Date
    May 2007
    Posts
    35
    Yes, all code that should be executed after the XML loads must somehow be triggered by the onLoad function, and no where else.

  18. #18
    Senior Member Ovaire d'Oz's Avatar
    Join Date
    May 2006
    Location
    France
    Posts
    148
    ok it'll be online by tomorrow. Thanks again.

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center