View Full Version : problems passing XML data to a variable
docoleg
12-23-2005, 04:24 AM
Problem: I am sure this problem has a very simple solution, but somehow I am missing it.
Here is the code:
mcq_xml = new XML();
mcq_xml.ignoreWhite = true;
mcq_xml.onLoad = function(success) {
if (success) {
trace(mcq_xml.firstChild.firstChild);
txt = mcq_xml.firstChild.firstChild;
return txt;
}
};
mcq_xml.load('example.xml');
trace (txt);
XML data is as follows (example.xml):
<tag>Example text</tag>
Question: why can't I pass the XML node value to variable txt?
Billy T
12-23-2005, 06:38 AM
you can...but you are tracing txt before it loads
tarjinder80
12-23-2005, 11:07 AM
mcq_xml = new XML();
mcq_xml.ignoreWhite = true;
mcq_xml.onLoad = function(success) {
if (success) {
trace(mcq_xml.firstChild.firstChild);
txt = mcq_xml.firstChild.firstChild.nodeValue;
trace(txt);
}
};
mcq_xml.load('example.xml');
'mcq_xml.firstChild.firstChild' returns object
'mcq_xml.firstChild.firstChild.nodeValue' returns string
Tarjinder
docoleg
12-23-2005, 05:06 PM
Hi guys! Thanks for our prompt reply!
I can see how one can manipulate data while staying within the onLoad function.
? How can I assign value to a variable txt and use it later, lets say in a class or in other functions? Any leads on that one?
docoleg
12-23-2005, 06:12 PM
While I was looking for a solution, I ran into this webpage (http://www.kirupa.com/web/xml/XMLwithFlash3.htm), which discusses the problem, I mentioned above. Here is a quote:
It's in the onLoad function where you pretty much do everything it is you need to do with your loaded XML content. You need it to populate a menu? Do it in the onLoad. Want to display your family tree? Do it in the onLoad... The onLoad is the key to handling loaded XML since it is at that point you actually have access to it. Anywhere else and you just may not have any XML to reference.
? Why does it have to happen in the onLoad? Has anybody tried to pass a variable outside the onLoad?
Billy T
12-24-2005, 12:26 AM
you can access txt outside it just has to have been loaded and set
mcq_xml = new XML();
mcq_xml.ignoreWhite = true;
mcq_xml.onLoad = function(success) {
if (success) {
trace(mcq_xml.firstChild.firstChild);
txt = mcq_xml.firstChild.firstChild.nodeValue;
traceText()
}
};
mcq_xml.load('example.xml');
function traceText(){
trace(txt);
}
docoleg
12-24-2005, 05:57 PM
Thanks Billy T. Now it works like a breeze. :D
I found another thread, which discusses the same problem. Click here for more information. (http://www.flashkit.com/board/showthread.php?threadid=589246)
Oh, I am so close to finishing this project I've been working for the last 2 months, but I have I more, and hopefully the LAST problem. AS Forum, please help!
var mcq_xml:XML = new XML();
mcq_xml.ignoreWhite = true;
mcq_xml.onLoad = function(success) {
if (success) {
showAnswers(mcq_xml);
}
};
mcq_xml.load('example.xml');
function showAnswers (xmlDoc_xml) {
myTextArray = new Array();
myTextArray = [xmlDoc_xml.childNodes[0].childNodes[0],
xmlDoc_xml.childNodes[1].childNodes[0]];
trace (myTextArray[0]); // traces Answer1
if (myTextArray[0] == "Answer1") {
trace ("Your answer is correct");
} else {
trace ("Your answer is incorrect");
}
}
The XML code is here:
<tag>Answer1</tag>
<tag>Answer2</tag>
Problem: When I trace the array, it indicates that myTextArray[0] contains "Answer1", but the if else loop for some reason puts out "Your answer is incorrect", instead of "Your answer is correct".
Question: WHHHHHHHHY?
docoleg
12-26-2005, 01:12 AM
Tupps helped me figure it out. He suggested the following change in the code:
You aren't comparing the same things. myTextArray[0] is an Xml Node.
if (myTextArray[0].nodeValue == "Answer1") {
}
Now it finally works! Thanks Tupps.
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.