A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: xml - reading values with nodevalue

  1. #1
    Senior Member
    Join Date
    Oct 2000
    Posts
    184

    Unhappy xml - reading values with nodevalue

    Hi

    Anyone help with a small problem?

    I'm trying to read xml into Flash.

    Here is my code:

    trace('Player Name = firstChild is: '+theRoot.firstChild.firstChild.attributes.Name);
    trace('Player Score = firstChild is: '+theRoot.firstChild.firstChild.firstChild.nodeVal ue);
    trace('Player School = firstChild is: '+theRoot.firstChild.firstChild.firstChild);

    Here is my xml:

    <?xml version="1.0" encoding="UTF-8" ?>
    <table>
    <highScore>
    <player Name="Jonny">
    <score>Points = 1000</score>
    <school>Haywood High</school>
    </player>
    </highScore>
    </table>

    Here are the results:

    Player Name = firstChild is: Jonny
    Player Score = firstChild is: null
    Player School = firstChild is: <score>Points = 1000</score>

    The node value of Player score is 1000 but I keep getting null.

    Please help someone...

  2. #2
    Senior Member
    Join Date
    Oct 2000
    Posts
    184
    [B]NB: The space in nodeValue isn't really there... just happened when I copied and pasted...

  3. #3
    Ryan Thomson EvolveDesigns's Avatar
    Join Date
    Oct 2001
    Location
    British Columbia
    Posts
    3,338
    try changing nodeValue to firstChild
    Evolve Designs Interactive Media
    the natural selection

  4. #4
    Senior Member
    Join Date
    Oct 2000
    Posts
    184

    Talking Thanks but how do I make the variable persist through my movie?

    Cheers for the reply,

    got it to work - here's my code:

    function parseXMLData(success:Boolean):Void {
    //make sure data is loaded and usable
    if (success && this.status == 0) {
    var theRoot:XMLNode = this.firstChild;
    trace(theRoot);
    trace('The root node is called '+theRoot.nodeName);
    trace('\tnumber of child nodes: '+theRoot.childNodes.length);
    trace('The root node¨s first child is '+theRoot.firstChild.nodeName);
    trace('\tnumber of child nodes: '+theRoot.firstChild.childNodes.length);
    trace('Player Name = '+theRoot.firstChild.firstChild.attributes.Name);
    trace('Player Score = '+theRoot.firstChild.firstChild.firstChild.childNo des[0]);
    trace('Player School = '+theRoot.firstChild.firstChild.firstChild.nextSib ling.childNodes[0]);
    } else {
    trace('problem loading data');
    trace('error code = '+this.status);
    }
    }
    var xmlScore:XML = new XML();
    xmlScore.ignoreWhite = true;
    xmlScore.load('highScores.xml');
    xmlScore.onLoad = parseXMLData;
    var arrScore:Array = new Array();
    //array code...
    arrScore.push(xmlScore.childNodes[count]);
    count++;
    if (count>=xmlScore.childNodes.length) {
    myArray.sortOn('score');
    }

    My problem is now that I can't use this on frame 20 without it being in an array. I think the array code is not working though it generates no errors. When I trace the variable arrScore it comes back as undefined...

    Can any body help me please???

    Thanks in advance

  5. #5
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    if you can simplify the xml, this is a whole lot easier -
    Code:
    var xmlScore:XML = new XML();
    xmlScore.ignoreWhite = true;
    xmlScore.load('highScores.xml');
    xmlScore.onLoad = parseXMLData;
    
    arrScore = new Array();
    
    function parseXMLData(){
    theRoot = this.firstChild;
    len = theRoot.childNodes.length;
    
    for(var n=0;n!=len;n++){
    var aNode = theRoot.childNodes[n].firstChild;
    obj = {};
    obj.Name = aNode.attributes.Name;
    obj.score = aNode.attributes.Score;
    obj.school = aNode.attributes.School;
    arrScore[n] = obj;
    }
    showIt();
    };
    
    function showIt(){
    len1 = arrScore.length;
    arrScore.sortOn("score", Array.DESCENDING | Array.NUMERIC);
    for(var n=0;n!=len1;n++){
    trace(arrScore[n].Name);
    trace(arrScore[n].score);
    trace(arrScore[n].school+newline);
    }
    };
    
    /*--outputs--
    Jonny
    8000
    Haywood High
    
    Bonny
    6000
    Daywood High
    
    Ronny
    4200
    Maywood High
    */
    
     /*--simplified xml--
    <? xml version="1.0" encoding="UTF-8" ?> 
    <table>
    
    <highScore>
    <player Name="Jonny" Score="8000" School="Haywood High" />
    </highScore>
    
    <highScore>
    <player Name="Ronny" Score="4200" School="Maywood High" />
    </highScore>
    
    <highScore>
    <player Name="Bonny" Score="6000" School="Daywood High" />
    </highScore>
    
    </table>
    */

  6. #6
    Senior Member
    Join Date
    Oct 2000
    Posts
    184
    Thanks dude... I'll modify my code tomorrow and let you know how I get on...

    You're a star as I didn't really know how to stuff the array with data so cheeeeeeeers.

    Let me get back to you

    M

  7. #7
    Senior Member
    Join Date
    Oct 2000
    Posts
    184

    Talking Script causing flash player to run slowly

    Hello again,

    Can I just say that I really like the bugs on your site and would be very interested in looking at the fla but I'll mail you from the site for that...

    I'm not having much joy with that script I'm afraid...

    The trace statements don't output at all and I get an error box asking if I want to terminate the script that's causing flash player to run slowly...

    Don't think I've done anything wrong but at a guess the loop through array elements is never ending and thus not tracing anything out. I've never seen that syntax (!=) before but surely it should be <=

    for(var n=0;n<=len1;n++){

    Just tried this and it makes no difference and still crashes Flash...

    Cheers for helping me tidy up the xml though. Its always good to be as clean as possible...

    Can you guess what's wrong please???

  8. #8
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    != is not equal to

    in the code i supplied i am using variable len
    len = theRoot.childNodes.length;
    for(var n=0;n!=len;n++){

    in the code in your last post you are using len1
    for(var n=0;n<=len1;n++){

    if you haven't specified len1 to equal a number, the
    loop will crash the compiler

  9. #9
    Senior Member
    Join Date
    Oct 2000
    Posts
    184

    Wink I thought it might be my fault...

    Hi There,

    Thanks for clarifying that but I now suspect I have egg on my face as the previous xml schema was still loaded into flash when I was testing your script. So at a guess when the flash player got the xcScore.trigger action it went haywire...

    Thanks for all your help - sorry to suggest your code was at fault and not me

    I haven't got time to correct it till later now but will post any difficulties...

    BTW - your site is cool...

  10. #10
    Senior Member
    Join Date
    Oct 2000
    Posts
    184

    Talking Still crashing... ???

    Hi there,

    Cheers for your patience and your time but I've tracked it down by process of elimination by commenting out blocks of code...

    this is the code that makes it crash:

    function parseXMLData() {
    theRoot = this.firstChild;
    len = theRoot.childNodes.length;
    for (var n = 0; n != len; n++) {
    var aNode = theRoot.childNodes[n].firstChild;
    obj = {};
    obj.Name = aNode.attributes.Name;
    obj.score = aNode.attributes.Score;
    obj.school = aNode.attributes.School;
    arrScore[n] = obj;
    }
    showIt();
    }

    If as you said len is not getting a value that would explain this. When I
    trace (len); it comes back as undefined so this is definitely the case. However I don't understand why len is undefined when it equals theRoot.childNodes.length;???

    Any ideas?? I must look pretty dumb but this is my first effort with xml and this thread has taught me a lot so cheers...

    Thanx buddy...

  11. #11
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    variable len will only return undefined if the xml format
    is different from that posted above

    try my test file and let me know the results

  12. #12
    Senior Member
    Join Date
    Oct 2000
    Posts
    184

    Cool Problem loading xml

    Hi there,

    Yes cheers I think it might work... haven't got time to test this till later as have to go to work now but maybe I'll do it at lunchtime.

    My thoughts are that there is definitely a problem loading the xml data so you're one step ahead of me again by posting a new xml file...

    Cheers Dude - I'll get this cracked before long and post back when I try your file...

  13. #13
    Senior Member
    Join Date
    Oct 2000
    Posts
    184

    Talking WOW!!! you are a genius!!!

    I'm bamboozled as to how this works without using an xml connector component object? - Didn't realise you could do the whole thing with actionscript... Been a right plonker trying to use the xmlConnector and component inspector and getting confused as to where that links to the actionscript...

    BRILLIANT YOU ARE A STAR!!! just know how much I appreciate this...

    That is half way home. I've now built a form for kids after the game finishes where they get to enter their details. The score is just a variable passed to the keyframe where the form is.

    When I click send the contents of the form fields appear in a trace statement. Is it possible to use this now to add them into the xml file and then sort the array so that only the top 5 display? I notice that the xmlConnector (which I still can't believe is redundant) can do both send and recieve so this must be possible somehow.

    I know you've gone out of your way with code so far but any pointers you can give me as to how to do this will be fantastic. It won't be long before I have to learn Actionscript3 so if I can figure this out now I'll stand a chance of getting somewhere.

    CHEERS

    M

  14. #14
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    Flash cannot write to a file on its own, it must use a server side script to do this.
    Search around the forum or Google for - php write xml file *flash

  15. #15
    Senior Member
    Join Date
    Oct 2000
    Posts
    184

    XML output

    I could write such a script in plain old ASP but I want this to be transferable between schools and not stuck on a webserver. My PHP is a bit sketchy so that limits me to a microsoft webserver.

    Is there no easy way to do this so the xml and flash can talk to each other? Why in that case, does the xml connector allow you to output? into a server side script I imagine...

    Actually it makes sense to me - I just wanted to distribute this as a .exe and have the high scores still work. I suppose it will have to just not use xml on this occasion. I have learned loads though so cheers for your effort.

    It will be an interesting thread for other people to read as well...

    Thanks for your time on this...

    M

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