|
-
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
-
Senior Member
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.
-
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
-
Senior Member
<highscore>
<player1>
<score1>100</score1>
<name>Matthew</name>
</player1>
</highscore>
-
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
-
Senior Member
I am not that much xml guru, but yes, that is how I think it should work.
-
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
-
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.
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|