PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Reading XML attributes correctly
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Reading XML attributes correctly

  1. #1
    Senior Member
    Join Date
    Jul 2001
    Location
    Planet Earth
    Posts
    298

    Reading XML attributes correctly

    Here's my xml file

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <gameData>
    	<category id='1' >
    		<name>Keeping your Credit Clean</name>
    		<questions>
    			<question id='1'>
    				<qText>What is the best way to stay out of debt</qText>
    				<answers>
    					<answer id='1' aText="Buy expensive stuff" correct='false' />
    					<answer id='2' aText="Finance a pizza" correct='false' />
    					<answer id='3' aText="Don't overspend" correct='true'/>
    				</answers>
    			</question>
    			<question id='2'>
    				<qText>What would you do for a Klondike bar?</qText>
    				<answers>
    					<answer id='1' aText="Three cartwheels and two jumping Jacks" correct='true' />
    					<answer id='2' aText="Wrestle an alligator" correct='false' />
    					<answer id='3' aText="Swan dive into a puddle of mud" correct='false'/>
    				</answers>
    			</question>
    		</questions>
    	</category>
    </gameData>
    Here's my code...
    Code:
    // loop thru the XML, populate field on screen with data from XML
    var i:int=1;
    for each (var cat:XML in gameXML.*){
    	// set the header of each category (this one only has one category)
    	this["h"+i].txt.text = cat.name;
    	
    	trace(cat.name);
    	for each (var q:XML in cat.questions.*){
    		trace(" " + q..@id + ") " + q.qText);
    		var nextQuestion:Object = new Object();
    		nextQuestion.question = q.qText;
    		nextQuestion.answers = new Array();
    		
    		for each (var a:XML in q.answers.*){
    	
    			trace("    " + a..@id + ") " + a..@aText);
    			nextQuestion.answers.push(a..@aText);
    		}
    		
    	}
    	
    	
    	i++;
    	
    }

    Here's my output....

    Code:
    Keeping your Credit Clean
     1123) What is the best way to stay out of debt
        1) Buy expensive stuff
        2) Finance a pizza
        3) Don't overspend
     2123) What would you do for a Klondike bar?
        1) Three cartwheels and two jumping Jacks
        2) Wrestle an alligator
        3) Swan dive into a puddle of mud
    The only problem is the 1123) and 2123). It should be 1 and 2. Please help
    ---
    Thinking outside of the box will get you fired if the "box" is strict budget.

  2. #2
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    The descendant (..) operator means returns an XMLList of every match you can find. Since you're asking for every attribute called "id" you are getting 4 results. Use that to your advantage and have E4X return one list at a time.

    Actionscript Code:
    for each (var cat:XML in gameXML..category){
      trace(cat.name);
      for each (var q:XML in cat..question){
        trace("\t" + q.@id + ") " + q.qText);
          for each ( var a:XML in q..answer){
            trace("\t\t" + a.@id + ") " + a.@aText);
          }
      }
    }

  3. #3
    Senior Member
    Join Date
    Jul 2001
    Location
    Planet Earth
    Posts
    298
    Thanks, you big ole code pimp!
    ---
    Thinking outside of the box will get you fired if the "box" is strict budget.

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