-
AS3 How to parse xml into an array
I can load the xml, but I'm having trouble parsing it.
Code:
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("fillinblank.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
myXML = new XML(e.target.data);
trace(myXML.QUESTION.INFINITIVE);
}
How do I turn this
PHP Code:
<WORKSHEET>
<QUESTION>
<NUMBER>"1."</NUMBER>
<BEGINNING>"Federico siempre"</BEGINNING>
<MIDDLE>"llega"</MIDDLE>
<END>"tarde."</END>
<INFINITIVE>"llegar"</INFINITIVE>
<ANSWER>"32894722213"</ANSWER>
</QUESTION>
</WORKSHEET>
into this?
Code:
var p1:Array=new Array("Federico siempre","llega","tarde.","llegar","32894722213");
-
Why would you want to do that? That Array is full of things with different semantics. Wouldn't it be better to parse it into a Question object and set fields that actually mean something?
-
Code:
var arr:Array = new Array();
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("fillinblank.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
myXML = new XML(e.target.data);
var len:uint = myXML.QUESTION.children().length();
for(var i:uint = 0; i < len; i++){
arr[i] = myXML.QUESTION.children()[i];
}
}
-
Thanks ilike2,
It worked perfectly.
If I have a varying number of questions in the xml, what is the best way to push the info into individual arrays?
PHP Code:
<WORKSHEET>
<QUESTION>
<BEGINNING>Federico siempre</BEGINNING>
<MIDDLE>llega</MIDDLE>
<END>tarde.</END>
<INFINITIVE>llegar</INFINITIVE>
<ANSWER>"32894722213"</ANSWER>
</QUESTION>
<QUESTION>
<BEGINNING>Carmen</BEGINNING>
<MIDDLE>canta</MIDDLE>
<END>con sus amigas.</END>
<INFINITIVE>cantar</INFINITIVE>
<ANSWER>"32894722213"</ANSWER>
</QUESTION>
</WORKSHEET>
I want to use nested arrays.
mainArray(arr1,arr2,arr3. etc)
Thanks
Last edited by Wisconsin; 04-07-2011 at 09:13 PM.
-
You can add the values to an associative array.
Code:
var arr:Array = new Array();
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("aaa.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
myXML = new XML(e.target.data);
var len:uint = myXML.QUESTION.length();
for(var i:uint = 0; i < len; i++){
arr.push({begin: myXML.QUESTION.child("BEGINNING")[i],
middle: myXML.QUESTION.child("MIDDLE")[i],
end: myXML.QUESTION.child("END")[i],
inf: myXML.QUESTION.child("INFINITIVE")[i],
answer: myXML.QUESTION.child("ANSWER")[i]});
}
}
-
Thanks ilike2flash, you're a great help.
How can I name the arrays in the loop so I get a nested array like this?
Code:
myNestedArray=(p1,p2,p3,p4 etc)
so I can reference the nested array p1[2], p2[3] etc
The number of questions will vary in the XML.
Last edited by Wisconsin; 04-09-2011 at 11:03 AM.
-
I can do something like this but it isn't dynamic and I know there's a better way. I'd appreciate any help.
Code:
var arr:Array = new Array();
var p1:Array = new Array();
var p2:Array = new Array();
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("fillinblank2.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
myXML=new XML(e.target.data);
var len:uint=myXML.QUESTION.length();
for (var i:uint = 0; i < len; i++) {
arr.push({begin: myXML.QUESTION.child("BEGINNING")[i],
middle: myXML.QUESTION.child("MIDDLE")[i],
end: myXML.QUESTION.child("END")[i],
inf: myXML.QUESTION.child("INFINITIVE")[i],
answer: myXML.QUESTION.child("ANSWER")[i]});
}
p1.push(myXML.QUESTION.child("BEGINNING")[0],myXML.QUESTION.child("MIDDLE")[0],myXML.QUESTION.child("END")[0],myXML.QUESTION.child("INFINITIVE")[0],myXML.QUESTION.child("ANSWER")[0]);
p2.push(myXML.QUESTION.child("BEGINNING")[1],myXML.QUESTION.child("MIDDLE")[1],myXML.QUESTION.child("END")[1],myXML.QUESTION.child("INFINITIVE")[1],myXML.QUESTION.child("ANSWER")[1]);
gotoAndPlay("next");
}
stop();
I need to end up with this.
Code:
myNestedArray=(p1,p2,p3,p4 etc)
-
Originally Posted by Wisconsin
Thanks ilike2flash, you're a great help.
How can I name the arrays in the loop so I get a nested array like this?
Code:
myNestedArray=(p1,p2,p3,p4 etc)
so I can reference the nested array p1[2], p2[3] etc
The number of questions will vary in the XML.
Its already nested in the associative array. What is the problem?
To access the values you simple do.
Code:
trace(arr[0].begin);
trace(arr[0].middle);
trace(arr[0].inf);
trace(arr[0].answer);
trace(arr[1].begin);
trace(arr[1].middle);
trace(arr[1].inf);
trace(arr[1].answer);
-
Thanks ilike2flash,
I'm trying to learn as I go. I'm trying to create an online worksheet for my students that will post to a SQL database when the students take it. I already had the file done but I decided to use XML so the other teachers in my department could easily modify the content. Here is an example (the first 5 questions are fully coded on this sheet) http://www.spanishspanish.com/protected/junk/test.html? The log-in is username aaaaaa password aaaaaa.
The first correct answer is llega. Here is what I would like to do. The number of questions on the sheet will correspond to the number of questions in the XML. I would like to shuffle the array that holds the questions. My current structure looks like this. myQuestions (p1,p2,p3, etc) I would like to reference the nested array like this myQuestions[2][0];
Thanks for any further help. I'd like to learn to structure the nested arrays in the loop to accommodate my current code since I have several other projects I'd like to convert to read XML.
I looked over your website. I'll be spending some time on there in the future. It looks like a great resource.
Just for reference, this is how I"m setting up my questions. I'm hoping to learn a better, more dynamic way of this this as well, based on the xml length.
Code:
var onenumber_txt:TextField=new TextField();
onenumber_txt.type=TextFieldType.DYNAMIC;
onenumber_txt.text="1.";
onenumber_txt.autoSize=TextFieldAutoSize.LEFT;
onenumber_txt.x=indent;
onenumber_txt.y=80;
onenumber_txt.selectable=false;
onenumber_txt.antiAliasType=AntiAliasType.ADVANCED;
addChild(onenumber_txt);
onenumber_txt.setTextFormat(myFormatBlue2);
onenumber_txt.defaultTextFormat=myFormatBlue2;
//
var onefirst_txt:TextField= new TextField();
onefirst_txt.type=TextFieldType.DYNAMIC;
onefirst_txt.text=myQuestions[0][0];
onefirst_txt.x=indent+onenumber_txt.textWidth+buffer;
onefirst_txt.y=80;
onefirst_txt.selectable=false;
onefirst_txt.antiAliasType=AntiAliasType.ADVANCED;
onefirst_txt.autoSize=TextFieldAutoSize.LEFT;
addChild(onefirst_txt);
onefirst_txt.setTextFormat(myFormatBlue1);
onefirst_txt.defaultTextFormat=myFormatBlue1;
//
var oneunderline_txt:TextField= new TextField();
oneunderline_txt.text="___________________";
oneunderline_txt.autoSize=TextFieldAutoSize.LEFT;
oneunderline_txt.x=indent+onenumber_txt.textWidth+onefirst_txt.textWidth+(buffer*2);
oneunderline_txt.y=80;
oneunderline_txt.height=25;
oneunderline_txt.selectable=false;
oneunderline_txt.antiAliasType=AntiAliasType.ADVANCED;
addChild(oneunderline_txt);
oneunderline_txt.defaultTextFormat=myFormatBlue1;
oneunderline_txt.setTextFormat(myFormatBlue1);
//
var oneanswer_txt:TextField= new TextField();
oneanswer_txt.type=TextFieldType.INPUT;
oneanswer_txt.maxChars=15;
oneanswer_txt.tabIndex=1;
oneanswer_txt.x=indent+onenumber_txt.textWidth+onefirst_txt.textWidth+(buffer*2);
oneanswer_txt.y=75;
oneanswer_txt.width=oneunderline_txt.textWidth+3;
oneanswer_txt.height=30;
oneanswer_txt.border=false;
oneanswer_txt.antiAliasType=AntiAliasType.ADVANCED;
addChild(oneanswer_txt);
oneanswer_txt.defaultTextFormat=myFormatGreen;
oneanswer_txt.setTextFormat(myFormatGreen);
//
var onelast_txt:TextField=new TextField();
onelast_txt.type=TextFieldType.DYNAMIC;
onelast_txt.text=myQuestions[0][2];
onelast_txt.autoSize=TextFieldAutoSize.LEFT;
onelast_txt.x=indent+onenumber_txt.textWidth+onefirst_txt.textWidth+oneunderline_txt.textWidth+(buffer*3);
onelast_txt.y=80;
onelast_txt.selectable=false;
onelast_txt.antiAliasType=AntiAliasType.ADVANCED;
addChild(onelast_txt);
onelast_txt.setTextFormat(myFormatBlue2);
onelast_txt.defaultTextFormat=myFormatBlue2;
//
var oneinfinitive_txt:TextField=new TextField();
oneinfinitive_txt.type=TextFieldType.DYNAMIC;
oneinfinitive_txt.text=myQuestions[0][3];
oneinfinitive_txt.x=indent+onenumber_txt.textWidth+onefirst_txt.textWidth+((oneunderline_txt.textWidth-oneinfinitive_txt.textWidth)/2)+buffer;
oneinfinitive_txt.y=97;
oneinfinitive_txt.selectable=false;
oneinfinitive_txt.antiAliasType=AntiAliasType.ADVANCED;
oneinfinitive_txt.height=25;
oneinfinitive_txt.autoSize=TextFieldAutoSize.LEFT;
addChild(oneinfinitive_txt);
oneinfinitive_txt.setTextFormat(myFormatBlack);
oneinfinitive_txt.defaultTextFormat=myFormatBlack;
-
Here's a link to a thread with an xml based quiz framework I threw together a while back. You should be able to use it to help you either rewrite or refactor your project.
http://board.flashkit.com/board/show...highlight=quiz
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
|