7 Replies Latest reply on Mar 2, 2009 2:57 PM by kglad

    Naming Objects with a value from XML node

    Arkabyt
      I’m trying to get the value of an XML node and use it to create an object. The problem seems to occur with the object constructor. The value of the node isn’t being used as the new object’s name no matter what I try to do.

      Here is my example.
        • 1. Re: Naming Objects with a value from XML node
          kglad Adobe Community Professional & MVP
          :

          var tl:MovieClip=this;

          ActList:Array = new Array;

          function onXmlLoad(success:Boolean) {
          if (success) {

          // make a handle to the root node in the xml
          var mainNode:XMLNode = xmlLoader.firstChild;

          var myId=mainNode.firstChild.attributes.id;

          //Next I want to create an object and give it the name of the mainNode.firstChild.attributes.id but I don't know how.

          tl[mainNode.firstChild.attributes.id] = new Object();
          }
          }
          • 2. Naming Objects with a value from XML node
            Arkabyt Level 1
            Thanks, but can you explain what you are doing when you code:

            tl[mainNode.firstChild.attributes.id] = new Object();

            what's the "tl"? Why a movieclip?
            • 3. Re: Naming Objects with a value from XML node
              kglad Adobe Community Professional & MVP
              tl ("tee el") is the timeline that contains your code. i'm just using it (and array notation) to help flash resolve a string (mainNode.firstChild.attributes.id) into an object.
              • 4. Re: Naming Objects with a value from XML node
                Arkabyt Level 1
                Thank you so much for your help. I'm just trying to understand the way AS works and I'm having a hard time parsing vars within other operations. I'm also trying to populate an array from the attributes of an xml node but I can't get the code to work either because its the same, I guess scope issue, with using a var as a name. Can you please tell me how I would get this to work?



                • 5. Re: Naming Objects with a value from XML node
                  kglad Adobe Community Professional & MVP
                  1. for (var aNode:XMLNode = mainNode.firstChild.firstChild; aNode != null; aNode=aNode.nextSibling) {

                  is that actually looping?

                  2. is myBikes.bikeList an array?

                  3. {i:i+1, str:aNode.attributes[str]}, isn't what you want

                  4. myBikes.bikeList.push(tempArray); doesn't make any sense inside that for-loop.

                  • 6. Re: Naming Objects with a value from XML node
                    Arkabyt Level 1
                    1. Yes that works.

                    2. Yes myBikes.bikeList is an array inside an object.

                    3. I know..it doesn't work. What I want to do is to parse the attributes in a node and create an array with the attribute node and value. Can this be done? if so how?

                    4. You are again correct. It doesnt work. the resulting array is huge. So can can I do #3 and pop that into an array?

                    Thanks so much for your help.
                    • 7. Re: Naming Objects with a value from XML node
                      kglad Adobe Community Professional & MVP
                      ok. well, i'm not sure what you're trying to do, but the following makes more sense so you should try it and see if does what you want:

                      if (mainNode.firstChild.hasChildNodes()) {
                      var tempArray:Array = new Array();
                      var ll:Number = mainNode.firstChild.childNodes.length;
                      for (var aNode:XMLNode = mainNode.firstChild.firstChild; aNode != null; aNode=aNode.nextSibling) {
                      if (aNode.nodeType == 1) {

                      for (var str in aNode.attributes) {
                      //trace(".attributes."+str+" = "+aNode.attributes[str]);
                      tempArray.push({str:aNode.attributes[str]});// using str here doesn't work. The attribute names are not resolved
                      }
                      }
                      }
                      myBikes.bikeList = tempArray;
                      }