4 Replies Latest reply on Jun 30, 2009 3:06 PM by Christopher Scrudder

    Dynamically inserting XML

    Christopher Scrudder

      I am trying to append onto a xml object dynamically.  The format of the XML is as follows

       

      <Graph>

           <Line name="---">

                <point x="---" y="---" />

                ...

                <point x="---" y="---" />
           </Line>

           <Line name="---">

                ...
           </Line>

           ...

      </Graph>

        • 1. Re: Dynamically inserting XML
          Christopher Scrudder Level 1

          Here is my current code:

              public function appendToLine( strName:String, aXPoints:Array, aYPoints:Array ){
                  //find the line in the xmlGraph
                  var appendPath:String = "myGraph.firstChild";
                  for(var currPoint:XMLNode = myGraph.firstChild; currPoint != null; currPoint = currPoint.nextSibling)
                  {

                    //traverse through the lines til you find the match
                      if( "Line_"+currPoint.attributes.name == strName ){
                          //appendPath is the location of the line that needs to be appended onto
                          break;
                      }
                      else{
                          appendPath = appendPath + ".nextSibling";
                      }
                  }

           


                  trace("Append Path: " + appendPath);
                  //append XML now has the node of the current line.
                  for( var i:Number = 0; i < aXPoints.length; i++ ){
                      // draw the line
                      this[strName].lineTo( aXPoints[i]/70 * 640, 480-aYPoints[i]/20000*480 );
                     
                     //create xml to append
                      var xmlTemp:XML = new XML();
                      xmlTemp.parseXML("<Point />");
                      var temp:XMLNode = xmlTemp.firstChild;
                      temp.attributes.x = aXPoints[i];
                      temp.attributes.y = aYPoints[i];

           

                      //append
                      this[appendPath].appendChild(xmlTemp);//not working
                      trace(appendPath);
                     
                      //testing appendChild();
                      myGraph.firstChild.appendChild(xmlTemp);

                     
                      trace("=-=-=-=-=-=-=-=-=-=");
                      trace(" ");
                      trace(myGraph);
                  }
              }

          • 2. Re: Dynamically inserting XML
            Christopher Scrudder Level 1

            Even though this[appendPath].appendChild(xmlTemp); does not work; if I type in the path manually myGraph.firstChild.nextSibling.appendChild(xmlTemp); the xml is placed properly.

             

            • 3. Re: Dynamically inserting XML
              Christopher Scrudder Level 1

              I was reading the CS3 livedocs & they had this listed

               

              Example

              The following example uses eval() to set properties for dynamically named movie clips. This ActionScript sets the _rotation property for three movie clips, called square1_mc, square2_mc, and square3_mc.

              for (var i = 1; i <= 3; i++) {
              setProperty(eval("square"+i+"_mc"), _rotation, 5);
              }

              You can also use the following ActionScript:

              for (var i = 1; i <= 3; i++) {
              this["square"+i+"_mc"]._rotation = -5;
              }

              I'm not sure how I could use setProperty in the manner how I am attacking this problem; anyone have an idea?  Or would I be using getProperty?
              • 4. Re: Dynamically inserting XML
                Christopher Scrudder Level 1

                I determined that I am not going to be able to work this functionality in by the deadline, going with the route by dynamically creating arrays.