8 Replies Latest reply on Sep 17, 2007 2:22 PM by SymTsb

    AS3 Loading XML and Display on Stage

    Level 7
      I haven't done much with AS3 .. just simple Flex stuff, but I have to load
      XML from an external URL and discovered this was much easier to get what I
      needed with AS3. My problem is the XML loads and I can trace till the cows
      come home, but I can't get the code right to display the information on the
      stage.

      Here is the loading code:

      import flash.net.*;
      import flash.events.*;
      import flash.display.*;
      import flash.geom.*;
      import flash.ui.*;
      import flash.utils.*;
      import flash.text.*;

      var xmlLoader:URLLoader = new URLLoader();
      var xmlData:XML = new XML();

      xmlLoader.addEventListener(Event.COMPLETE, LoadXML);

      xmlLoader.load(new URLRequest("externalURLremoved"));

      function LoadXML(e:Event):void {

      xmlData = new XML(e.target.data);
      ParseWeather(xmlData);

      }

      function ParseWeather(weatherInput:XML):void {

      trace("XML Output");
      trace("------------------------");
      var weatherChildren:XMLList = weatherInput.cc.children();
      for each (var info:XML in weatherChildren) {
      trace(info);
      }
      }

      All the data displays in the output window perfectly. But I need to display
      values from the XML on the stage. With AS2, I would have used the variable
      box in the property inspector, but of course, this doesn't work in AS3. I
      can display text values like this:

      var input_txt:TextField = new TextField();
      input_txt.x = 105;
      input_txt.y = 35;
      input_txt.width = 530;
      input_txt.height = 156;
      addChild(input_txt);
      input_txt.text = "Hello World";

      but I can't figure out how to substitute a value from the XML for the text.
      All the sample I have found for loading XML stop with the trace statement in
      the output window. I haven't found any that take that through to the stage
      and I have looked online for hours and in every book that I have.

      I thought it would be something like
      input_txt.text=(info.tmp);

      but that throws errors. Everything I have tried throws errors.

      Any advice?

      Thanks!
      Nancy


        • 1. Re: AS3 Loading XML and Display on Stage
          Ardy15jan
          for each (var info:XML in weatherChildren) {
          trace(info);
          }


          Try using "input_txt.appendText(info);" instead of "trace(info);"...
          • 2. Re: AS3 Loading XML and Display on Stage
            Level 7
            It still doesn't display.

            First I got an error that input_txt was undefined, so I defined the variable
            and it doesn't throw errors now, but it also doesn't put anything on the
            stage.

            This is what I now have:

            trace("XML Output");
            trace("------------------------");
            var weatherChildren:XMLList = weatherInput.cc.children();
            var input_txt:TextField = new TextField;
            for each (var info:XML in weatherChildren) {
            input_txt.appendText(info);
            }

            I also tried putting a dynamic text field on the stage and naming it
            input_txt and that doesn't do anything either.

            I need to have things like

            Temperature: thetemperature

            where thetemperature is the dynamic number from the XML feed. The whole
            feed is displaying in the output window .. what I can't do is get values
            from that window to display on the stage.

            Thanks for your help!
            Nancy



            • 3. Re: AS3 Loading XML and Display on Stage
              Level 7
              this is frustrating ..

              I had what I thought would do it .. but I keep getting this error ..

              a conflict exists with definition tmpfield in namespace internal.


              I have tried changing the name .. changing the definition .. changing the
              movie .. changing everything and this keeps coming up no matter what I
              change the definition name to. Why is this?

              Thanks,
              Nancy


              • 4. Re: AS3 Loading XML and Display on Stage
                SymTsb Level 2
                When handling XML, displaying the data from a load (assuming you know the exact structure) simply requires calling the name of the element using dot syntax. The attached code is a very basic example of this. E4X has some other very interesting features two of which I've tried to show since they tend to be the most pertinent when handling XML.

                • 5. Re: AS3 Loading XML and Display on Stage
                  Level 7
                  Thank you! I do have it working now .. however, your code is much cleaner
                  than mine so I'll study this thoroughly and learn. Thanks so much!

                  Nancy

                  "SymTsb" <webforumsuser@macromedia.com> wrote in message
                  news:fckqgb$3bl$1@forums.macromedia.com...
                  > When handling XML, displaying the data from a load (assuming you know the
                  > exact
                  > structure) simply requires calling the name of the element using dot
                  > syntax.
                  > The attached code is a very basic example of this. E4X has some other
                  > very
                  > interesting features two of which I've tried to show since they tend to be
                  > the
                  > most pertinent when handling XML.
                  >
                  >
                  >
                  > import flash.display.*;
                  > import flash.text.*;
                  >
                  > var xmlString:String = "<gamescore><username>Mike Garcia</username><score
                  > number='10716' /></gamescore>";
                  > var xmlData:XML = new XML( xmlString );
                  >
                  > var myTF:TextField = new TextField();
                  > myTF.width = 250;
                  > myTF.height = 20;
                  >
                  > var myTF2:TextField = new TextField();
                  > myTF2.width = 250;
                  > myTF2.height = 20;
                  > myTF2.y = 100;
                  >
                  > // Display both text fields
                  > // myTF shows how to assign the value of a text node to a text field
                  > // myTF2 shows how to assign the value of an attribute to a text field
                  > myTF.text = xmlData.username;
                  > myTF2.text = xmlData.score.@number;
                  > addChild(myTF);
                  > addChild(myTF2);
                  >


                  • 6. Re: AS3 Loading XML and Display on Stage
                    Level 7
                    Well .. finally all done .. and uploaded and it doesn't work online. I have
                    a hunch it's probably the crossdomain thingie I have read about .. ::sigh::

                    Thanks all for the help.
                    Nancy


                    • 7. Re: AS3 Loading XML and Display on Stage
                      Level 7
                      That was it .. all solved and working nicely on the site.

                      Thanks again, everyone!
                      Nancy

                      "Nancy - Adobe Comm. Expert" <nancy@webwish.com> wrote in message
                      news:fcl4g1$cpo$1@forums.macromedia.com...
                      > Well .. finally all done .. and uploaded and it doesn't work online. I
                      > have a hunch it's probably the crossdomain thingie I have read about ..
                      > ::sigh::
                      >
                      > Thanks all for the help.
                      > Nancy
                      >
                      >


                      • 8. Re: AS3 Loading XML and Display on Stage
                        SymTsb Level 2
                        You are very welcome Nancy!