Note that there are some explanatory texts on larger screens.

plurals
  1. POXML - targeting node attribute, push into a Flash AS3 array
    primarykey
    data
    text
    <p>I'm trying to push just the contents of the "txt" attribute in each "question" tag into an array named "questions" in AS3 Flash. Here is an excerpt from my xml file.</p> <pre><code>&lt;question id='Q1' uId='99036' no_ans='2' txt='In a flat structure employees are not expected to provide their bosses with their opinions.' feedback='' type='MC' passingWeight='1' url='media/'&gt; &lt;answer id='Q1A1' uId='311288' txt='True' weight='0'/&gt; &lt;answer id='Q1A2' uId='311289' txt='False' weight='1'/&gt; &lt;/question&gt; &lt;question id='Q2' uId='99037' no_ans='2' txt='In a hierarchy, information typically flows downward.' feedback='' type='MC' passingWeight='1' url='media/'&gt; &lt;answer id='Q2A1' uId='311290' txt='True' weight='1'/&gt; &lt;answer id='Q2A2' uId='311291' txt='False' weight='0'/&gt; &lt;/question&gt; &lt;question id='Q3' uId='99038' no_ans='2' txt='Someone who keeps many projects going at one time is an example of someone who is flexible-time oriented.' feedback='' type='MC' passingWeight='1' url='media/'&gt; &lt;answer id='Q3A1' uId='311292' txt='True' weight='1'/&gt; &lt;answer id='Q3A2' uId='311293' txt='False' weight='0'/&gt; &lt;/question&gt; </code></pre> <p>Here is my attempt at a loop:</p> <pre><code>// get number of questions trace(myXML.question.length()); numberOfQuestions = myXML.question.length(); //loop and push questions into questions array at top for (var i:int = 0; i &lt; numberOfQuestions; i++) { trace("Hello."); questions.push(myXML.question.@txt); trace(questions); } </code></pre> <p>This just pushes all 9 of the questions at once into each position of the array. I wanted 1 question per array position. I'm not sure how to use the id attribute in the question tag to differentiate each question.</p> <p><strong>EDIT:</strong> I tried this and I can access the questions texts using <strong>getQuestionAt(2)</strong> from within the <strong>processXML</strong> function, but not outside of it.</p> <pre><code>var myXML:XML; var myLoader:URLLoader = new URLLoader(); myLoader.load(new URLRequest("html/VUBZ7318CROSSCULTUREQUIZ/manifest.xml")); myLoader.addEventListener(Event.COMPLETE, processXML); function processXML(e:Event):void { myXML = new XML(e.target.data); //trace(myXML.question) // get number of questions trace(myXML.question.length()); numberOfQuestions = myXML.question.length(); //Question list var questions:Object = {}; //Extracting question from xml for each (var item:XML in myXML.question) { questions[item. @ id] = item. @ txt; } //Some method for fetching question from question list function getQuestionAt( index:Number ):String { if (questions["Q" + index] == undefined) { throw new Error("Wrong index for question!!!"); } return questions["Q"+index]; } //Getting question from list trace( "Here is question No 2:\t" + getQuestionAt(2) ); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload