24 Replies Latest reply on Jan 14, 2010 10:06 PM by kglad

    Fill textfield with greater value from an xml file

    baianomauricioct

      Hi all, I have an actionscript pulling data from an xml file. As you can see in the code below, I have textFields been created on the fly.

      I need to have the "resultsField" textfield to be programatically filled with the greater value from either "column1" or "column2".

       

      Thank you all for the greater service!

       

       

      --------------------- XML  ------------------------

      <?xml version="1.0" encoding="utf-8" ?>
      <cats>
          <title>
              USD
          </title>
          <cat>
              0.5879
          </cat>
          <cat>
              0.9875
          </cat>
      </cats>

       

       

       

       

      --------------------- ACTIONSCRIPT 2.0  ------------------------

       

      this.createTextField("column1",25,0,50,200,30);
      this.createTextField("column2",25,325,50,200,30);
      this.createTextField("resultsField",20,0,105,200,30);

       

      column1.html = true;
      column2.html = true;
      resultsField.html = true;

       

      var my_xml = new XML();
      my_xml.onLoad = function(success) {

       

          if (success) {
              RootNode = my_xml.firstChild;
              column1.htmlText = RootNode.childNodes[1].firstChild;
              column2.htmlText = RootNode.childNodes[2].firstChild;

       

              trace(column1.text);
              trace(column2.text);
          } else {
              trace("buah!");
          }

       

      };
      my_xml.ignoreWhite = true;
      my_xml.load("quotes.xml");

       


      resultsField.htmlText = "higher Value here";

        • 1. Re: Fill textfield with greater value from an xml file
          kglad Adobe Community Professional & MVP

          use:

           


           

          this.createTextField("column1",25,0,50,200,30);
          this.createTextField("column2",25,325,50,200,30);
          this.createTextField("resultsField",20,0,105,200,30);

           

          column1.html = true;
          column2.html = true;
          resultsField.html = true;

           

          var my_xml = new XML();
          my_xml.onLoad = function(success) {

           

              if (success) {
                  RootNode = my_xml.firstChild;
                  column1.htmlText = RootNode.childNodes[1].firstChild;
                  column2.htmlText = RootNode.childNodes[2].firstChild;

          resultsField.htmlText = Math.max(Number(column1.text),Number(column2.text));

           

                  trace(column1.text);
                  trace(column2.text);
              } else {
                  trace("buah!");
              }

           

          };
          my_xml.ignoreWhite = true;
          my_xml.load("quotes.xml");

           


          //resultsField.htmlText = "higher Value here";

          • 2. Re: Fill textfield with greater value from an xml file
            baianomauricioct Level 1

            Thanks for your VERY prompt feedback, but it is giving me a "NaN" inside the resultsField textField.

            • 3. Re: Fill textfield with greater value from an xml file
              kglad Adobe Community Professional & MVP

              use:

              this.createTextField("column1",25,0,50,200,30);
              this.createTextField("column2",26,325,50,200,30);
              this.createTextField("resultsField",20,0,105,200,30);

              column1.html = true;
              column2.html = true;
              resultsField.html = true;

              var my_xml = new XML();
              my_xml.onLoad = function(success) {
                  if (success) {
                      RootNode = my_xml.firstChild;
                      column1.htmlText = RootNode.childNodes[1].firstChild;
                      column2.htmlText = RootNode.childNodes[2].firstChild;
                      resultsField.htmlText = Math.max(Number(stripF(column1.text)),Number(stripF(column2.text)));
                  } else {
                      trace("buah!");
                  }
              };
              my_xml.ignoreWhite = true;
              my_xml.load("test.xml");

              function stripF(s:String):String{
                  return s.split(" ").join("").split("\n").join("").split("\r").join("");
              }
              • 4. Re: Fill textfield with greater value from an xml file
                baianomauricioct Level 1

                Thank you very much, it now works fine.

                Can I use more than two fields with this?

                 

                Thank you.

                • 5. Re: Fill textfield with greater value from an xml file
                  kglad Adobe Community Professional & MVP

                  yes, but Math.max only accepts 2 parameters so you'll want to create your own function to return the max of several numbers:

                   

                  function maxF(a:Array):Number{

                  var max:Number = a[0];

                  for(var i:Number=1;i<a.length;i++){

                  if(a[i]>max){

                  max=a[i];

                  }

                  }

                  return max;

                  }

                   

                  p.s.  please mark this thread as answered.

                  • 6. Re: Fill textfield with greater value from an xml file
                    baianomauricioct Level 1

                    Thank you very much. I will play a bit with this function you've sent over.

                    You were very much of a helper .... I appreciate it!

                    • 7. Re: Fill textfield with greater value from an xml file
                      baianomauricioct Level 1

                      Hi Kglad. I really appreciate your help.

                       

                      I tried the code you've sent me, but I didn't succeed into implementing it. See if you could take a look at this:

                       

                      this.createTextField("column1",this.getNextHighestDepth(),0,50,200,30);
                      this.createTextField("column2",this.getNextHighestDepth(),325,50,200,30);
                      this.createTextField("column3",this.getNextHighestDepth(),0,80,200,30);
                      this.createTextField("column4",this.getNextHighestDepth(),325,80,200,30);
                      this.createTextField("resultsField",this.getNextHighestDepth(),200,125,200,30);

                       

                      column1.html = true;
                      column2.html = true;
                      column3.html = true;
                      column4.html = true;
                      resultsField.html = true;

                       

                      var my_xml = new XML();
                      my_xml.onLoad = function(success) {

                       

                          if (success) {
                              RootNode = my_xml.firstChild;
                              column1.htmlText = RootNode.childNodes[1].firstChild;
                              column2.htmlText = RootNode.childNodes[2].firstChild;
                              column3.htmlText = RootNode.childNodes[3].firstChild;
                              column4.htmlText = RootNode.childNodes[4].firstChild;
                              resultsField.htmlText = Math.max(Number(stripF(column1.text)), Number(stripF(column2.text)));

                       

                              trace(column1.text);
                              trace(column2.text);
                          } else {
                              trace("buah!");
                          }

                       

                      };
                      my_xml.ignoreWhite = true;
                      my_xml.load("quotes.xml");

                       

                      function maxF(a:Array):Number {
                          var max:Number = a[4];
                          for (var i:Number = 1; i<a.length; i++) {
                              if (a[i]>max) {
                                  max = a[i];
                              } else {
                                  trace("Nah!!!");
                              }
                          }
                          return max;
                      }

                       

                      /*
                      function stripF(s:String):String {
                      return s.split(" ").join("").split("\n").join("").split("\r").join("");
                      }
                      */

                       

                      Thanks.

                      • 8. Re: Fill textfield with greater value from an xml file
                        kglad Adobe Community Professional & MVP

                        use:

                         


                         

                        this.createTextField("column1",this.getNextHighestDepth(),0,50,200,30);
                        this.createTextField("column2",this.getNextHighestDepth(),325,50,200,30);
                        this.createTextField("column3",this.getNextHighestDepth(),0,80,200,30);
                        this.createTextField("column4",this.getNextHighestDepth(),325,80,200,30);
                        this.createTextField("resultsField",this.getNextHighestDepth(),200,125,200,30);

                         

                        column1.html = true;
                        column2.html = true;
                        column3.html = true;
                        column4.html = true;
                        resultsField.html = true;

                         

                        var my_xml = new XML();
                        my_xml.onLoad = function(success) {

                         

                            if (success) {
                                RootNode = my_xml.firstChild;
                                column1.htmlText = RootNode.childNodes[1].firstChild;
                                column2.htmlText = RootNode.childNodes[2].firstChild;
                                column3.htmlText = RootNode.childNodes[3].firstChild;
                                column4.htmlText = RootNode.childNodes[4].firstChild;


                                resultsField.htmlText = maxF([Number(stripF(column1.text)),Number(stripF(column2.text)),Number(stripF(column3.tex t)),Number(stripF(column4.text))]);

                         

                                trace(column1.text);
                                trace(column2.text);
                            } else {
                                trace("buah!");
                            }

                         

                        };
                        my_xml.ignoreWhite = true;
                        my_xml.load("quotes.xml");

                         

                        function maxF(a:Array):Number {
                            var max:Number = a[0];
                            for (var i:Number = 1; i<a.length; i++) {
                                if (a[i]>max) {
                                    max = a[i];
                                }
                            }
                            return max;
                        }
                        function stripF(s:String):String {
                        return s.split(" ").join("").split("\n").join("").split("\r").join("");
                        }


                        • 9. Re: Fill textfield with greater value from an xml file
                          baianomauricioct Level 1

                          AWESSOOOOOOMMEE!! Thank you ... but now that I bothered you that long ...

                          Would you have a better way to re-write this:

                           

                          this.createTextField("column1",this.getNextHighestDepth(),0,50,200,30);
                          this.createTextField("column2",this.getNextHighestDepth(),325,50,200,30);
                          this.createTextField("column3",this.getNextHighestDepth(),0,80,200,30);
                          this.createTextField("column4",this.getNextHighestDepth(),325,80,200,30);

                           

                          I have 30 fields I have to create programmatically. I don't mind wrting all of them, but if there is a better way ....might as well apply it.

                           

                          Thanks again for the help!

                          • 10. Re: Fill textfield with greater value from an xml file
                            kglad Adobe Community Professional & MVP

                            :

                             

                            var tl:MovieClip = this;
                            var colnum:Number = 2;

                            var xSpace:Number = 200;

                            var ySpace:Number = 80
                            for(var i=1;i<=30;i++){
                                tl.createTextField("column"+i,tl.getNextHighestDepth(),xSpace*((i-1)%colnum),ySpace*Math. floor((i-1)/colnum),200,30);
                                tl["column"+i].html = true;
                            }
                            tl.createTextField("resultsField",tl.getNextHighestDepth(),xSpace,ySpace*Math.floor(i+coln um/colnum),200,30);
                            resultsField.html = true;

                             

                            var my_xml = new XML();
                            my_xml.onLoad = function(success) {
                                if (success) {
                                    var a:Array = [];
                                    RootNode = my_xml.firstChild;
                                    for(var i=1;i<=30;i++){
                                        tl["column"+i].htmlText = RootNode.childNodes[i].firstChild;
                                        a.push(Number(stripF(tl["column"+i].text)));
                                    }
                                    resultsField.htmlText = maxF(a);
                                } else {
                                    trace("buah!");
                                }
                            };
                            my_xml.ignoreWhite = true;
                            my_xml.load("quotes.xml");

                             

                            function maxF(a:Array):Number {
                                var max:Number = a[0];
                                for (var i:Number = 1; i<a.length; i++) {
                                    if (a[i]>max) {
                                        max = a[i];
                                    }
                                }
                                return max;
                            }
                            function stripF(s:String):String {
                                return s.split(" ").join("").split("\n").join("").split("\r").join("");
                            }

                            • 11. Re: Fill textfield with greater value from an xml file
                              baianomauricioct Level 1

                              Very good job!

                               

                              I wish I was good with actionscript as you are. --:]

                               

                              One last question then I won't be bothering you anymore. ... I SWEAR!!

                               

                              Remeber you helped me get the code to retrive the value from the xml where had the maximum value?

                              How can I get the parent from that value programmatically. I mean, this is the xml file

                               

                              <?xml version="1.0" encoding="utf-8" ?>
                              <cats>
                                  <title>
                                      USD
                                  </title>
                                  <cat>
                                      0.1879
                                  </cat>
                                  <cat>
                                      0.2875
                                  </cat>
                                  <cat>
                                      0.3879
                                  </cat>
                                  <cat>
                                      0.4875
                                  </cat>
                              </cats>

                               

                              How can I find out programatically the USD and print it on the screen. Keep in mind we already know that the last value is the greater, how can I get inside other nodes [inside the same parent -cats-] based on the knowledge of the greater value? I don't know if I am clear in my question.

                              Knowing the last cat is the greater, how can I get into -title-?

                              • 12. Re: Fill textfield with greater value from an xml file
                                kglad Adobe Community Professional & MVP

                                if you're trying to retrieve the value of the "title" node, use:

                                 

                                stripF(RootNode.childNodes[0].firstChild.nodeValue);

                                 

                                where RootNode is as you defined it.

                                1 person found this helpful
                                • 13. Re: Fill textfield with greater value from an xml file
                                  baianomauricioct Level 1

                                  Very nice .. Thank you!


                                  • 15. Re: Fill textfield with greater value from an xml file
                                    baianomauricioct Level 1

                                    at some point I got lost in translation .. now the min [former max] value is showing me a NaN again .. can you do a quick look in  the whole code to see where I have it messed up?

                                     

                                    I think the bold text in the code is where I messed it up ... could you please make sure it is correct?

                                     

                                     

                                    Thnks.

                                     

                                    -------------------------------------------------------------------
                                    var tl:MovieClip = this;
                                    var colnum:Number = 2;
                                    var xSpace:Number = 200;
                                    var ySpace:Number = eval("column"+i);
                                    for (var i = 1; i<=30; i++) {
                                        tl.createTextField("column"+i,tl.getNextHighestDepth(),xSpace*((i-1)/colnum),ySpace,200,3 0);
                                        tl["column"+i].html = true;
                                    }
                                    tl.createTextField("resultsField",tl.getNextHighestDepth(),xSpace,ySpace*Math.floor(i+coln um/colnum),200,30);
                                    resultsField.html = true;

                                     

                                    resultsField.html = true;
                                    this.createTextField("moeda",this.getNextHighestDepth(),325,80,200,30);

                                     

                                    var my_xml = new XML();
                                    my_xml.onLoad = function(success) {

                                     

                                        if (success) {
                                            for (var i = 1; i<=30; i++) {
                                                RootNode = my_xml.firstChild;
                                                eval("column"+i).htmlText = RootNode.childNodes[i].firstChild;
                                                resultsField.htmlText = minF([Number(stripF("column"+i.text))]);
                                                moeda.htmlText = stripF(RootNode.childNodes[0].firstChild.nodeValue);
                                                //trace(column10._y);
                                                //trace(column20._x);
                                                trace(resultsField.text);
                                                trace(stripF(RootNode.childNodes[0].firstChild.nodeValue));
                                            }
                                        } else {
                                            trace("buahCol!");
                                        }

                                     

                                    };
                                    my_xml.ignoreWhite = true;
                                    my_xml.load("quotes.xml");

                                     

                                    function minF(a:Array):Number {
                                        var min:Number = a[0];
                                        for (var i:Number = 1; i>a.length; i++) {
                                            if (a[i]>min) {
                                                min = a[i];
                                            } else {
                                                trace("buahMin!");
                                            }
                                        }
                                        return min;

                                     

                                    }
                                    function stripF(s:String):String {
                                        return s.split(" ").join("").split("\n").join("").split("\r").join("");
                                    }

                                    • 16. Re: Fill textfield with greater value from an xml file
                                      kglad Adobe Community Professional & MVP

                                      :

                                       


                                       

                                      -------------------------------------------------------------------
                                      var tl:MovieClip = this;
                                      var colnum:Number = 2;
                                      var xSpace:Number = 200;
                                      var ySpace:Number = eval("column"+i);
                                      for (var i = 1; i<=30; i++) {
                                          tl.createTextField("column"+i,tl.getNextHighestDepth(),xSpace*((i-1)/colnum),yS pace,200,30);
                                          tl["column"+i].html = true;
                                      }
                                      tl.createTextField("resultsField",tl.getNextHighestDepth(),xSpace,ySpace*Math.fl oor(i+colnum/colnum),200,30);
                                      resultsField.html = true;

                                       

                                      resultsField.html = true;
                                      this.createTextField("moeda",this.getNextHighestDepth(),325,80,200,30);

                                       

                                      var my_xml = new XML();
                                      my_xml.onLoad = function(success) {

                                       

                                          if (success) {

                                      var a:Array = [];
                                              for (var i = 1; i<=30; i++) {
                                                  RootNode = my_xml.firstChild;
                                                  eval("column"+i).htmlText = RootNode.childNodes[i].firstChild;
                                                  a.push(Number(stripF(tl["column"+i].text)));
                                                  moeda.htmlText = stripF(RootNode.childNodes[0].firstChild.nodeValue);
                                                  //trace(column10._y);
                                                  //trace(column20._x);
                                                  trace(resultsField.text);
                                                  trace(stripF(RootNode.childNodes[0].firstChild.nodeValue));
                                              }

                                      resultsField.htmlText=minF(a);
                                          } else {
                                              trace("buahCol!");
                                          }

                                       

                                      };
                                      my_xml.ignoreWhite = true;
                                      my_xml.load("quotes.xml");

                                       

                                      function minF(a:Array):Number {
                                          var min:Number = a[0];
                                          for (var i:Number = 1; i>a.length; i++) {
                                             if (a[i]<min) {
                                                  min = a[i];
                                              } else {
                                                  trace("buahMin!");
                                              }
                                          }
                                          return min;

                                       

                                      }
                                      function stripF(s:String):String {
                                          return s.split(" ").join("").split("\n").join("").split("\r").join("");
                                      }

                                      1 person found this helpful
                                      • 17. Re: Fill textfield with greater value from an xml file
                                        baianomauricioct Level 1

                                        Beautiful!

                                        THANK YOU SO MUCH!!!

                                        • 19. Re: Fill textfield with greater value from an xml file
                                          baianomauricioct Level 1

                                          Hi,

                                           

                                          how hard would it be to assign a stylesheet to those textfields?

                                           

                                          I have this style sheet done already, but have no clue how to associate them with the dinamic textfield

                                           

                                           

                                          stylish = new TextField.StyleSheet();

                                          stylish.load("coreasset/currency.css");

                                           

                                          I wanted to assign a class called ".day" to them.

                                          • 20. Re: Fill textfield with greater value from an xml file
                                            kglad Adobe Community Professional & MVP

                                            use an onLoad() handler to assign your textfield's styleSheet property to stylish:

                                             

                                            stylish.onLoad=function(s:Boolean){

                                            if(s){

                                            yourtf.styleSheet=stylish;

                                            }

                                            }

                                            • 21. Re: Fill textfield with greater value from an xml file
                                              baianomauricioct Level 1

                                              ok, thanks .. the first part I got . Now how can I assign a class to my TFs?

                                              What I tried is not working, see in bold below:

                                               

                                              for (var i = 1; i<=15; i++) {

                                              var RootNode = getXml.firstChild;

                                              var currency = RootNode.firstChild.childNodes[1].firstChild;

                                              eval("day"+i).htmlText = "<p class='day'>"+RootNode.childNodes[i].childNodes[4].firstChild.firstChild+"</p>";

                                              eval("price"+i).htmlText = RootNode.childNodes[i].childNodes[4].childNodes[2].firstChild;

                                              eval(("day"+i)+15).htmlText = RootNode.childNodes[i+15].childNodes[4].firstChild.firstChild;

                                              eval(("price"+i)+15).htmlText = RootNode.childNodes[i+15].childNodes[4].childNodes[2].firstChild;

                                              a.push(Number(stripF(tl["day"+i].text)));

                                              currentPrice.htmlText = RootNode.firstChild.childNodes[4].childNodes[2].firstChild;

                                              currencyName.htmlText = currency+"&#47;USD";

                                              currencyDate.htmlText = RootNode.firstChild.childNodes[4].firstChild.firstChild;

                                              trace(RootNode.firstChild.childNodes[4].childNodes[1].firstChild);

                                              //trace(day10._y);

                                              //trace(currentPrice.text);

                                              //trace(stripF(RootNode.firstChild.childNodes[0].firstChild.nodeValue));

                                              }

                                              • 22. Re: Fill textfield with greater value from an xml file
                                                kglad Adobe Community Professional & MVP

                                                do you have a "day" style?

                                                 

                                                are you applying that style AFTER your stylesheet completes loading and you've assigned your textfield's styleSheet property.  if you think you have, show the code that ensures the ss loads and is applied before trying to reference the "day" style.

                                                • 23. Re: Fill textfield with greater value from an xml file
                                                  baianomauricioct Level 1

                                                  Yes, I do have a day class inside my css.

                                                  This is the code I have:

                                                   

                                                  //CSS Handler

                                                  stylish = new TextField.StyleSheet();

                                                  stylish.load("coreasset/currency.css");

                                                   

                                                  stylish.onLoad = function(success:Boolean) {

                                                  if (success) {

                                                  tl["day"+i].styleSheet = stylish;

                                                  }

                                                  };

                                                   

                                                  //loading XML into Dinamic fields

                                                  getXml.onLoad = function(success) {

                                                   

                                                  if (success) {

                                                  var a:Array = [];

                                                  for (var i = 1; i<=15; i++) {

                                                  var RootNode = getXml.firstChild;

                                                  var currency = RootNode.firstChild.childNodes[1].firstChild;

                                                  eval("day"+i).htmlText = "<p class='regular'>"+RootNode.childNodes[i].childNodes[4].firstChild.firstChild+"</p>";

                                                  eval("price"+i).htmlText = RootNode.childNodes[i].childNodes[4].childNodes[2].firstChild;

                                                  eval(("day"+i)+15).htmlText = RootNode.childNodes[i+15].childNodes[4].firstChild.firstChild;

                                                  eval(("price"+i)+15).htmlText = RootNode.childNodes[i+15].childNodes[4].childNodes[2].firstChild;

                                                  a.push(Number(stripF(tl["day"+i].text)));

                                                  currentPrice.htmlText = RootNode.firstChild.childNodes[4].childNodes[2].firstChild;

                                                  currencyName.htmlText = currency+"&#47;USD";

                                                  currencyDate.htmlText = RootNode.firstChild.childNodes[4].firstChild.firstChild;

                                                  trace(RootNode.firstChild.childNodes[4].childNodes[1].firstChild);

                                                  //trace(day10._y);

                                                  //trace(currentPrice.text);

                                                  //trace(stripF(RootNode.firstChild.childNodes[0].firstChild.nodeValue));

                                                  }

                                                  lowestPeriod.htmlText = minF(a);

                                                  } else {

                                                  trace("buahCol!");

                                                  }

                                                   

                                                  };

                                                  • 24. Re: Fill textfield with greater value from an xml file
                                                    kglad Adobe Community Professional & MVP

                                                    put your load xml statement in your stylish.onLoad() method.

                                                     

                                                    if that doesn't close this thread, start a new thread.