A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Highscores - XML

  1. #1
    An FKer
    Join Date
    Sep 2005
    Location
    Ontario
    Posts
    1,167

    Highscores - XML

    Hey everyone

    I have a question. Im attempting to make a high score list for this one game using XML and ASP.

    Well its going well so far, I can have an XML file, read it in Flash and things like that

    But Im having a small problem of reading specific elements of the XML, this is what the file looks like:

    <?xml version="1.0"?>
    <highscore>
    <player1>Matthew</player1>
    <score1>100</score1>
    </highscore>



    All I want to do in Flash (for now, for the sake of learning) is to load that xml file and read the element <player1> from a trace

    This is the flash I have (in a button):
    on (release) {
    my_xml = new XML();
    myLoadxml = new XML();
    my_xml.load("C:\\Inetpub\\wwwroot\\games\\highscor e.xml");
    //
    //
    highElement = my_xml.firstChild;
    playerElement = highElement.firstChild;
    playerText = playerElement.firstChild;
    value = playerText.nodeValue;
    trace(value);
    //
    //
    my_xml.onLoad = function(success) {
    if (success) {
    my_xml.contentType = "text/xml";
    my_xml.xmlDecl = "<?xml version=\"1.0\" ?>";
    my_xml.sendAndLoad ("http://localhost/games/test.asp", myLoadxml);
    } else {
    }
    };
    }



    Basically when I click the button, the trace should display the variable 'value', which is the text of playerText, which is the child of playerElement and so on up the hierarchy list

    Heres the list :
    XML file
    - highElement
    - playerElement
    - playerText
    - value


    Anyways, if that made ANY sense at all, I would really appreciate some help so I can get this highscores to work

    Thanks

  2. #2
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Shouldnt you wait for xml file to be loaded before reading the value? All the xml manipulation is usually done in its xml.onLoad function, not straight after you used load command.

  3. #3
    An FKer
    Join Date
    Sep 2005
    Location
    Ontario
    Posts
    1,167
    Oh .. haha sorry, Im new to this

    Ok, so I moved the code to the .onLoad function like so :

    my_xml.onLoad = function(success) {
    if (success) {
    my_xml.contentType = "text/xml";
    my_xml.xmlDecl = "<?xml version=\"1.0\" ?>";
    my_xml.sendAndLoad("http://localhost/games/test.asp", myLoadxml);

    highElement = my_xml.firstChild;
    playerElement = highElement.firstChild;
    playerText = playerElement.firstChild;
    value = playerText.nodeValue;
    trace(value);
    }
    };


    So now it should be reading the value once its loaded the xml (I think), but I still get an 'undefined' value for trace

    I tested this with an XML that Flash creates itself and it worked fine ... so Im not sure whats going on

  4. #4
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    <highscore>
    <player1>
    <score1>100</score1>
    <name>Matthew</name>
    </player1>
    </highscore>

  5. #5
    An FKer
    Join Date
    Sep 2005
    Location
    Ontario
    Posts
    1,167
    Ok *Reads*

    Well first of all, dont feel obligated to reply anymore, I know Im being a pain from asking all these questions

    So basically :

    <highscore>
    <player1>
    <score1>100</score1>
    <name>Matthew</name>
    </player1>
    </highscore>



    highElement = my_xml.firstChild; -------- <highscore>
    playerElement = highElement.firstChild; --------- <player1>
    playerText = playerElement.firstChild; ---------<score1>
    value = playerText.nodeValue; ------------ 100
    trace(value);


    .. right?

    ps, I know I should be asking this in the XML forum, but no one was online there at the time and I was hoping to get this small part working before I leave. Otherwise Ill forget what I did on monday haha

  6. #6
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    I am not that much xml guru, but yes, that is how I think it should work.

  7. #7
    An FKer
    Join Date
    Sep 2005
    Location
    Ontario
    Posts
    1,167
    Oh ok

    Well Im still getting an "undefined" when I trace(value);, so Im still not sure whats wrong

    But thanks for helping

    Ill try the XML forums and wait for a reply there

  8. #8
    Senior Member
    Join Date
    Aug 2004
    Location
    San Diego, California
    Posts
    421
    You were almost there. You just needed another firstChild


    highElement = my_xml.firstChild;
    playerElement = highElement.firstChild;
    playerText = playerElement.firstChild;
    -->scoreText = playerText.firstChild;
    -->value = scoreText.nodeValue;

    trace(value);


    I believe that should work fine.

  9. #9
    An FKer
    Join Date
    Sep 2005
    Location
    Ontario
    Posts
    1,167
    Oh haha, this whole child business is pretty confusing, perhaps I should get into the habit of writing out the tree somewhere so I can see it physically

    Thanks for the help, though I wont be able to try your idea until this monday since Im away to attend a wedding

    Ill be sure to do it on monday, and hopefully it will work

    Many thanks

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