A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: reading xml in as3 - Newbe to as3 here

  1. #1
    Senior Member
    Join Date
    May 2005
    Location
    Wisconsin
    Posts
    181

    Question reading xml in as3 - Newbe to as3 here

    I got some of this right but im lost on the script in red below. I attached the files so you can see the error message. if you can explain what im doing wrong too that would be great so i can learn.


    http://www.wisc-online.com/prototypes/JGame/shooter.zip


    Code:
    var XmlId:String = new String();
    
    //Arrays to hold question data
    var qst:Array=new Array([]);
    var ans:Array = new Array();
    var val:Array = new Array();
    
    
    var ObjTitle:String = new String();
    var ObjIntro:String = new String();
    
    //Holds question Length
    var arrarylength:Number = new Number();
    
    
    var loader:URLLoader = new URLLoader();
    loader.addEventListener(Event.COMPLETE, loadXML);
    loader.load(new URLRequest("2152.xml"));
    
    function loadXML(e:Event):void {
    
    	XML.ignoreWhitespace=true;
    	var xml=new XML(e.target.data);
    
    	arrarylength=xml.question.length();
    
    	ObjTitle=xml.@Title;
    	ObjIntro=xml.@Title;
    
    	var i:Number = new Number();
    	i=0;
    
    	//++++++++++++ BUILD A 2 DIM ARRAY ++++++++++++++++++++++
    	for (i=0; i<arrarylength; i++) {
    
    		ans[i] = new Array();
    		val[i] = new Array();
    
    	}
    	//+++++++++++++++++++ END +++++++++++++++++++++++++++++++
    
    	//++++++++++++++++ get answers ++++++++++++++++++++++++++
    	i=0;
    	var j:Number = new Number();
    	
    	var ValAttributes:XMLList=xml.question.answer.@correct;
    	for each (var correct:XML in ValAttributes) {
    
    		for(j=0;j<4;j++){
    			val[i][j]=unescape(correct);
    			trace(val[i][j]);
    		}
    		i++
    		
    	}
    	//+++++++++++++++++++++ end =++++++++++++++++++++++++++++
    	
    	
    	//++++++++++++++++++ Get Questions ++++++++++++++++++++++
    	i=0;
    	var QstAttributes:XMLList=xml.question.@txt;
    	//trace(QstAttributes)
    	for each (var txt:XML in QstAttributes) {
    		qst[i]=unescape(txt);
    		//trace("ij");
    		i++;
    	}
    	//++++++++++++++++++ End ++++++++++++++++++++++++++++++++
    	
    }
    
    stop();

    xml Sheet
    Code:
    <questionGroup Title="Name the Presidents" Intro="In this game, you will be asked to identify the U.S. President being described.  Answer as quickly as possible to earn the most points.  ">
    
    <question txt="I wrote a book rejecting the divinity of Jesus." ><answer correct="false" txt="Jimmy Carter"/><answer correct="true" txt="Thomas Jefferson"/><answer correct="false" txt="Barack Obama"/><answer correct="false" txt="Abraham Lincoln"/></question>
    
    <question txt="I had to borrow money to go to my own inauguration."><answer correct="false" txt="Rutherford B. Hayes"/><answer correct="false" txt="Abraham Lincoln"/><answer correct="false" txt="William J. Clinton"/><answer correct="true" txt="George Washington"/></question>
    </questionGroup>
    Josh
    Multimedia Programmer
    flashmajic.com

  2. #2
    rabid_Delineator AttackRabbit's Avatar
    Join Date
    Dec 2003
    Location
    Orlando, Florida
    Posts
    481
    try this

    Code:
    var XmlId:String = new String();
    
    //Arrays to hold question data
    var qst:Array = new Array();
    var ans:Array = new Array();
    var val:Array = new Array();
    
    
    var ObjTitle:String = new String();
    var ObjIntro:String = new String();
    
    //Holds question Length
    var arrarylength:Number = new Number();
    
    
    var loader:URLLoader = new URLLoader();
    loader.addEventListener(Event.COMPLETE , loadXML );
    loader.load(new URLRequest("2152.xml"));
    
    function loadXML(e:Event):void {
    
    	XML.ignoreWhitespace=true;
    	var xml=new XML(e.target.data);
    
    	arrarylength=xml.question.length();
    
    	ObjTitle=xml.@Title;
    	ObjIntro=xml.@Title;
    
    	var i:Number;
    	i=0;
    
    	//++++++++++++ BUILD A 2 DIM ARRAY ++++++++++++++++++++++
    	//for (i=0; i<arrarylength; i++) {
    		//trace( "hello : " + ans );
    		//ans[i].push( new Array( 4 ) );
    		//val[i].push( new Array( 4 ) );
    
    	//}
    	//+++++++++++++++++++ EDN +++++++++++++++++++++++++++++++
    
    	//++++++++++++++++ get answers ++++++++++++++++++++++++++
    	i=0;
    	var j:Number;
    	
    	var ValAttributes:XMLList=xml.question.answer.@correct;
    	for each (var correct:XML in ValAttributes) {
    		var tmpArray:Array = new Array();
    		for(j=0;j<4;j++){
    			tmpArray[ j ]=unescape(correct);
    		}
    		val[ i ] = tmpArray;
    		i++		
    	}
    	trace( val[0][1] );
    	trace( val[1][2] );
    	//+++++++++++++++++++++ end =++++++++++++++++++++++++++++
    	
    	
    	//++++++++++++++++++ Get Questions ++++++++++++++++++++++
    	i=0;
    	var QstAttributes:XMLList=xml.question.@txt;
    	for each (var txt:XML in QstAttributes) {
    		qst[i]=unescape(txt);
    		i++;
    	}
    	//++++++++++++++++++ End ++++++++++++++++++++++++++++++++
    	
    }
    
    stop();
    The main thing is the way you were trying to create a mutlidimensional array. Its easier just to create the array in your loop , plus you can rid out that other for loop. Some other notes, you dont need to create a new Number() once you have declared it.

  3. #3
    Senior Member
    Join Date
    May 2005
    Location
    Wisconsin
    Posts
    181
    That worked perfect thanks for your help and the advice.
    Josh
    Multimedia Programmer
    flashmajic.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center