Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Nested for loops error

New Here ,
Jul 01, 2013 Jul 01, 2013

Hi all Community members,, I am facing problem with pulling attributes from a xml which I have created. I am using nested for loop. Please kindly help me out to solve this problem....

Thanks in advance...

Below is the code & xml ...


import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.*;
import flash.net.*;

var myXML:XML;

var voiceOver:String;
var startOffset:Number;
var slideData:Array;
var id_cont_array:Array = new Array();
var id_textfield_array:Array = new Array();
var id_image_array:Array = new Array();

var myXML_Container_Length:Number;
var myXML_textField_Length:Number;
var myXML_images_Length:Number;

var loader:URLLoader = new URLLoader();
loader.load(new URLRequest("dynamic.xml"));

loader.addEventListener(Event.COMPLETE,processXML);

function processXML(e:Event):void
{
slideData=new Array();
myXML = new XML(loader.data);

loader.removeEventListener(Event.COMPLETE, processXML);
loader.removeEventListener(IOErrorEvent.IO_ERROR, loadError);

myXML = new XML(e.target.data);
voiceOver = myXML. @ voice_over;
startOffset = myXML. @ start_offset;

for (var i : uint = 0; i<myXML.*.length(); i++)
{
  //trace(myXML + "slide");
}


//myXML_List = myXML.children();
myXML_Container_Length = myXML.children().length();
//trace(myXML_Container_Length+ "==myXML_Container_Length")
//myXML_textField_Length = myXML.containerss[0].children().length();
//myXML_images_Length = myXML.children().length();
//trace(myXML.child("containerss")[3]. @ id.toXMLString()+ "==myXML.containerss[0]. @ id;");
//trace(myXML.containerss[0].textfield[0]. @ id+ "==myXML.slide.containerss[0]. @ id");
//trace(myXML.containerss[0].textfield[1]. @ id+ "==myXML.slide.containerss[1]. @ id");
//trace(myXML.containerss[3].image[0]. @ id + "==image[0]");
//trace(myXML.containerss[0].children() + "==myXML.containerss[0].children()");

for (var j:Number=0; j<=myXML_Container_Length; j++)
{
  id_cont_array.push(myXML.containerss. @ id);
  //trace(myXML.containerss.children().length() + "==j");
  myXML_textField_Length = myXML.containerss.children().length();
  trace(myXML_textField_Length + "==myXML_textField_Length");
  for (var k:Number=0; k<=myXML_textField_Length-1; k++)
  {

   id_textfield_array.push(myXML.containerss.textfield. @ id);
   //id_image_array.push(myXML.containerss.image. @ id);
   trace(id_textfield_array + "==id_textfield_array==" + k + "---j==" + j);
   //trace(id_image_array + "==id_image_array");
  }
  //trace(id_textfield_array+"==id_cont_array");
}

//trace(myXML_List.length() + "===myXML_List");

}

function loadError(e:IOErrorEvent):void
{

trace("loadError");
}

/// XML is as given below..

<slide orientation="VERTICAL" voice_over="Slide1.mp3" start_offset="0" margin="">

<containerss id="0" orientation="HORIZONTAL" padding="10" margin="10" x="10" y="160" width="200" height="150">
  <textfield id="Text1" text="textfieldText1" padding="10" margin="10"  x="10" y="160"/>
  <image id="img1" text="" padding="10" margin="10" x="10" y="160"/>
  <textfield id="Text2" text="textfieldText2" padding="10" margin="10" x="10" y="160"/>
</containerss>
<containerss id="1" orientation="HORIZONTAL" padding="10" margin="10" width="200" height="200" x="10" y="160">
  <textfield id="Text3" text="textfieldText3" padding="10" margin="10" x="10" y="160"/>
  <image id="img2" text="" padding="10" margin="10" x="10" y="160"/>
 
</containerss>
<containerss id="2" orientation="VERTICAL" padding="10" margin="10" width="200" height="150" x="10" y="160">
  <textfield id="Text5" text="textfieldText5" padding="10" margin="10" width="200" height="150" x="10" y="160"/>
  <image id="img3" text="" padding="10" margin="10" x="10" y="160"/>
  <textfield id="Text6" text="textfieldText6" padding="10" margin="10" x="10" y="160"/>
</containerss>
<containerss id="4" orientation="VERTICAL" padding="10" margin="10" width="200" height="150" x="10" y="160">
  <textfield id="Text7" text="textfieldText7" padding="10" margin="10" width="200" height="150" x="10" y="160"/>
  <image id="img4" text="" padding="10" margin="10" x="10" y="160"/>
  <textfield id="Text8" text="textfieldText8" padding="10" margin="10" width="200" height="150" x="10" y="160"/>
</containerss>
</slide>

TOPICS
ActionScript
1.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jul 01, 2013 Jul 01, 2013

Parsing is totally wrong. You function processXML() should be like that:

function processXML(e:Event):void

{

          slideData = new Array();

          loader.removeEventListener(Event.COMPLETE, processXML);

          loader.removeEventListener(IOErrorEvent.IO_ERROR, loadError);

          myXML = new XML(e.target.data);

          voiceOver = myXML.@voice_over;

          startOffset = myXML.@start_offset;

          myXML_Container_Length = myXML.children().length();

          var node:XMLList;

          f

...
Translate
LEGEND ,
Jul 01, 2013 Jul 01, 2013

What specifically is your problem?  What portion of the code you show is not working for you?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 01, 2013 Jul 01, 2013

The project is as follows:

I am working on a project where "text", "image", "video" loads dynamically. The properties of these objects is also specified in the xml.

And in the above code I am getting error :

TypeError: Error #1010: A term is undefined and has no properties.

I guess the error may be in the line:: id_textfield_array.push(myXML.containers.textfield. @ id);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 01, 2013 Jul 01, 2013

Go into your Flash Publish Settings and select the option to Permit Debugging.  The error message might then be enhanced in that it might indicate the specific line of code from which the error arises.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 01, 2013 Jul 01, 2013

The following is not going to process XML properly:

for (var k:Number = 0; k <= myXML_textField_Length - 1; k++)

{

               id_textfield_array.push(myXML.containerss.textfield.@id);

}

because containerss node's children are textfeild and image and you loop through the node as if all cildren have node name textfield. So, there is no textfield[2]. Hence the error.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 01, 2013 Jul 01, 2013

Actually, there are many more problems with your parsing.

This line:

for (var j:Number = 0; j <= myXML_Container_Length; j++)

should be

for (var j:Number = 0; j <= myXML_Container_Length - 1; j++)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 01, 2013 Jul 01, 2013

Parsing is totally wrong. You function processXML() should be like that:

function processXML(e:Event):void

{

          slideData = new Array();

          loader.removeEventListener(Event.COMPLETE, processXML);

          loader.removeEventListener(IOErrorEvent.IO_ERROR, loadError);

          myXML = new XML(e.target.data);

          voiceOver = myXML.@voice_over;

          startOffset = myXML.@start_offset;

          myXML_Container_Length = myXML.children().length();

          var node:XMLList;

          for (var j:Number = 0; j <= myXML_Container_Length - 1; j++)

          {

                    id_cont_array.push(myXML.containerss.@id);

                    node = myXML.containerss.children();

                    myXML_textField_Length = node.length();

                    for (var k:Number = 0; k <= myXML_textField_Length - 1; k++)

                    {

                              switch (node.name().toString())

                              {

                                        case "textfield":

                                                  trace(node.@id);

                                                  id_textfield_array.push(node.@id);

                                                  break;

 

                                        case "image":

                                                  id_image_array.push(node.@id);

                                                  break;

                              }

                    }

          }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 01, 2013 Jul 01, 2013
LATEST

And this is a much better way to deal with parsing - note there are no indexed loops:

function processXML(e:Event):void

{

          slideData = new Array();

          loader.removeEventListener(Event.COMPLETE, processXML);

          loader.removeEventListener(IOErrorEvent.IO_ERROR, loadError);

          myXML = new XML(e.target.data);

          voiceOver = myXML.@voice_over;

          startOffset = myXML.@start_offset;

          var node:XML;

          for each (var child:XML in myXML.children())

          {

                    id_cont_array.push(child.@id);

                    for each (node in child.children())

                    {

                              switch (node.name().toString())

                              {

                                        case "textfield":

                                                  id_textfield_array.push(node.@id);

                                                  break;

 

                                        case "image":

                                                  id_image_array.push(node.@id);

                                                  break;

                              }

                    }

          }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines