PDA

View Full Version : XML problem


hannahd
06-14-2005, 08:39 PM
Hello,

I have written a class for loading questions from an xml file.

My problem is with the process method which is called when the XML file is loaded. Trace displays the content of the xml file when I put trace(this), so 'this' means the xml object, but when I ask for the firstChild of this, it says it is not a property of it.

Please help!

Hannah

class QuizXMLLoader {

private var xmlobj:XML;
private var quiz_url:String;
private var questions:Array;

function QuizXMLLoader(quiz_url:String) {
this.quiz_url = quiz_url;
xmlobj = new XML();
xmlobj.load(quiz_url);
xmlobj.ignoreWhite = true;
xmlobj.onLoad = process;
questions = new Array();
}

function process(success:Boolean) {
trace(this);

//var question_nodes:Array = this.firstChild.childNodes;

//for(var i=0; i<question_nodes.length; i++) {
//var question_node:XMLNode = question_nodes[i];
//var fields = question_node.childNodes;

//var question_text:String = String(fields[0].firstChild);
//var answer_nodes:Array = fields[1].childNodes;

//var answers:Array = new Array();
//for(var j=0; j<answer_nodes.length; j++) {
//answers.push(String(answer_nodes[j].firstChild));
//}

//var correct_answer = Number(String((fields[2].firstChild)));

//var answer1 = answers[0];
//var answer2 = answers[1];
//var answer3 = answers[2];
//var answer4 = answers[3];

//var q = new Question(question_text, answer1, answer2, answer3, answer4, correct_answer);
//questions.push(q);

//}
}

}

sleekdigital
06-14-2005, 08:50 PM
what does your xml look like?

hannahd
06-14-2005, 08:54 PM
<?xml version="1.0"?>
<questions>
<question>
<question_text>What is the capital of England?</question_text>
<answers>
<answer>London</answer>
<answer>Manchester</answer>
<answer>Birmingham</answer>
<answer>York</answer>
</answers>
<correctanswer>1</correctanswer>
</question>
</questions>

sleekdigital
06-14-2005, 09:51 PM
hmmm.... what happens if you use xmlobj instead of this? And I would wrap the code inside of the process function in an if(success) just to be sure it is loading properly. Nothing else is jumping out at me right now, can you copy and paste the exact error message you are getting?

hannahd
06-14-2005, 10:02 PM
I have found a web page explaining everything, but I don't really understand it!

http://www.flashkit.com/board/showthread.php?s=&threadid=580028

I have exactly the same problem as this person.

I get the error message:

**Error** C:\Flash_practice\QuizXMLLoader.as: Line 17: There is no property with the name 'firstChild'.
trace(this.firstChild);

Total ActionScript Errors: 1 Reported Errors: 1

I tried putting xmlobj instead of this, but it didn't work unfortunately. :(

Thanks for your help :)

Hannah

sleekdigital
06-14-2005, 10:54 PM
I personally use something like method number four from the one site mentioned there ...
http://www.kirupa.com/web/xml/XMLspecificIssues3.htm

But at the moment, I don't see why this.firstChild.childNodes doesn't work. At any rate, it is a good idea to follow one of the methods outlined in that thread on flash kit. Pick one and see what you can do with it and ask specific questions as needed

Volga
06-15-2005, 01:49 PM
Did you try to remove <?xml version="1.0"?>