Reputation: 5
I created an XML file named "stage1.txt" in the folder "musics". The XML File is :
<music>
<speed>10</speed>
<time>
<note>1</note>
<note>12</note>
<note>32</note>
<note>41</note>
</time>
<where>
<lane>3</lane>
<lane>2</lane>
<lane>1</lane>
<lane>4</lane>
</where>
</music>
And then in the flash file I used the following code to recall the XML file :
and then in the flash file I used the following code to get the data.
var myXML:XML = new XML();
myXML.ignoreWhite=true;
myXML.load("musics/stage"+_global.stages+".xml");
var temp = 0, temp2 = 0;
myXML.onLoad = function(success){
if (success){
trace (myXML);
}
}
It worked fine until here. However, I wanted to recall the first value of the XML file, the "speed". I tried using this code :
var speed = myXML.firstChild.firstChild.nodeValue;
but it doesn't seem to work. Tried other things like :
myXML.firstChild.childNodes[0].nodeValue
but doesn't work too.
Upvotes: 0
Views: 3563
Reputation: 6722
You should have used a third party library to convert XML to Object - you may use Greensock's XMLParser - there is a short tutorial and sample code.
Your code could look like this
import gs.dataTransfer.XMLParser;
XMLParser.load("musics/stage"+_global.stages+".xml", onFinish);
function onFinish($success:Boolean, $results:Object, $xml:XML):Void {
if ($success) {
trace("The speed is: "+$results.speed[0].nodeValue);1
}
}
So then you can refer to the nodes of your XML in much more convenient (and robust) way: myMXL.speed[0].nodeValue
.
Referring to the nodes in a way you try to is very bad practice. If the XML changes your code will break which is bad...
Upvotes: 0
Reputation: 5212
Add an extra firstChild in there. I'm not really sure why, but it seems the parser handles the document as a whole as being a seperate level.
The best way to find the stuff you need when things aren't working out is to trace and see what it contains. So start with the xml object ('trace(myXML)') and traverse from there on 'trace(myXML.firstChild)' -> 'trace(myXML.firstChild.firstChild)' -> etc. until you find the info you need.
Upvotes: 1