9 Replies Latest reply on Aug 10, 2006 6:06 PM by injpix

    Loading XML into dynamic text boxes

    tbar1
      I have an XML document and I am trying to load the text from that doc into corresponding dynamic text boxes in flash. I wanted to use a loop to do this because I have 30+ textboxes to load text into and I thought this would be easiest to do. I know that the XML is being parsed and I can get it into the dynamic text boxes without the loop so I know the loop I'm trying to use is the problem. The dynamic text boxes are named "page1box0", "page1box1", "page1box2", etc. Here is my AS code (and I'm using Flash 8):


      //create new xml object
      xmlDoc = new XML();
      //ignore white space in xml doc
      xmlDoc.ignoreWhite = true;
      //used this in a loop to ensure that xml doc loaded
      xmlDoc.onLoad = function(success) {
      if (success) {
      //trace(this);
      j=0;
      while (j <= xmlDoc.firstChild.firstChild.firstChild.childNodes.length) {
      currentXML = xmlDoc.firstChild.firstChild.firstChild.childNodes[j].firstChild.nodeValue;
      trace ("currentXML = " + currentXML);
      currentBlank = _root["page1box" + j + "_txt"];
      trace ("j = " + j);
      currentBlank = currentXML;
      j++;
      }
      //trace ("currentXML = " + currentXML);
      } else {
      trace("xml not loaded");
      }
      };

        • 1. Re: Loading XML into dynamic text boxes
          injpix Level 3
          You mention that the instance name format are like the following;
          "page1box0",
          "page1box1",
          "page1box2", etc.

          But in your code your have;

          currentBlank = _root["page1box" + j + "_txt"];

          Notice the “_txt” suffix.
          • 2. Re: Loading XML into dynamic text boxes
            tbar1 Level 1
            I was using the _txt just to try and see if that would work. But I also tried:
            _root["page1box" + j]

            and that didn't work either. I should probably have mentioned that all of the text boxes are located in a movie clip, but all of the AS code is also in that same movie clip.
            • 3. Re: Loading XML into dynamic text boxes
              injpix Level 3
              If I understand what you are saying then it’s a scope issue. Try;

              this["page1box"+j]
              • 4. Re: Loading XML into dynamic text boxes
                tbar1 Level 1
                Thanks for the suggestion, unfortunately that didn't work either. I put in a trace after the line:

                currentBlank = _root["page1box" + j];
                and the change you suggested:
                currentBlank = this["page1box" + j];

                and with both the var "currentBlank" came back undefined.
                • 5. Re: Loading XML into dynamic text boxes
                  injpix Level 3
                  Ah…we forgot to add the .text property for the path. Also it seems to be that Flash can’t have a dynamic path to be assigned to a variable and then have it used else where. I thought that this would be possible with brackets, perhaps I am wrong. However I got a simplified version from your code working;

                  j=0;
                  while (j <= 5)
                  {
                  currentXML = "stuff stuff stuff"+j;
                  trace ("currentXML = " + currentXML);

                  this["page1box"+j].text = currentXML;
                  j++;
                  }

                  But you might still have a scope looking at your last post.
                  So you have a movieclip on stage with roughly 30 textboxes inside of it, correct?
                  And inside this movieclip you have the AS code in the timeline, correct?

                  If so then my previous post should be correct.
                  • 6. Re: Loading XML into dynamic text boxes
                    tbar1 Level 1
                    Yes all of my text boxes are in a movie clip and all of the AS is in the movie clip. So I tried the following:

                    j=0;
                    while (j <= xmlDoc.firstChild.firstChild.firstChild.childNodes.length) {
                    currentXML = xmlDoc.firstChild.firstChild.firstChild.childNodes[j].firstChild.nodeValue;
                    trace ("currentXML = " + currentXML);
                    this["page1box"+j].text = currentXML;
                    trace ("this = " + this["page1box" + j].text);
                    //currentBlank = currentXML;
                    trace ("j = " + j);
                    j++;

                    And I also tried:

                    j=0;
                    while (j <= xmlDoc.firstChild.firstChild.firstChild.childNodes.length) {
                    currentXML = xmlDoc.firstChild.firstChild.firstChild.childNodes[j].firstChild.nodeValue;
                    trace ("currentXML = " + currentXML);
                    currentBlank = this["page1box"+j];
                    currentBlank = currentXML;
                    trace ("currentBlank = " + currentBlank);
                    //currentBlank = currentXML;
                    trace ("j = " + j);
                    j++;

                    And neither of these worked.
                    • 7. Re: Loading XML into dynamic text boxes
                      everynewday

                      Would something like this work?

                      for (var j = 0; j <= xmlDoc.firstChild.firstChild.firstChild.childNodes.length; j++) {
                      currentXML = xmlDoc.firstChild.firstChild.firstChild.childNodes[j].firstChild.nodeValue;
                      trace ("currentXML = " + currentXML);
                      this["page1box"+j].text = currentXML;
                      trace ("this = " + this["page1box" + j].text);
                      //currentBlank = currentXML;
                      trace ("j = " + j);
                      }
                      • 8. Re: Loading XML into dynamic text boxes
                        tbar1 Level 1
                        Tried that one too with no success. When I trace "this...." it comes back undefined.
                        • 9. Re: Loading XML into dynamic text boxes
                          injpix Level 3
                          I still think it is a scope issue tbar1. You mentioned that when you trace the following;

                          trace ("this = " + this["page1box" + j].text);

                          …it comes back “undefined”? That tells me that the textfield is in the wrong place or the instance name is incorrect. If that doesn’t help you, if possible post the files online.