-
Senior Member
[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.
-
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
-
Senior Member
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.
-
Bearded (M|G)od
You need to cast it to a Number type:
var myNum:Number = Number("1");
trace(myNum+1); //traces 2
-
Senior Member
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.
-
"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)
-
Senior Member
Thanks, but brackets does not change the result. 18 should not be a string, I need it as a number.
-
Senior Member
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...
-
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);
-
Senior Member
wooohooooo! It works!!! Thank you!
-
Senior Member
Although, I still have an undefined returned with the second trace.
-
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.
-
Senior Member
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);
-
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);
}
-
Senior Member
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?
-
Senior Member
ok! It's working perfectly.
Thanks a lot.
-
Yes, all code that should be executed after the XML loads must somehow be triggered by the onLoad function, and no where else.
-
Senior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|