13 Replies Latest reply on Jun 22, 2007 8:40 AM by ChrisFlynn

    XML Preloader

      Hi all

      I'm going to be faced with trying to load a 3000+ node XML file into Flash.

      With something that big, ideally, I would like to incorporate some sort of preloader in Flash, so as the XML loads, you can see its progress.

      But I'm wondering if showing load progress for an XML file will actually work. Its always been my experience that Flash can only load XML as fast as the XML will load in a browser, but Flash doesn't show anything loaded until the whole XML is loaded.

      There is, in the Flash Help Files, an example of getBytesLoaded() and getBytesTotal() for XML files, but those don't seem to work at all.

      Searches on Google only come across other people asking the same questions with little or no answers.

      So I'm wondering if this is even possible. Can Flash show the progress of a loading XML file, before the XML file has finished loading? If so, where can I find how to do this?

      Thanks again for the help
        • 1. Re: XML Preloader
          kglad Adobe Community Professional & MVP
          i've done it without problem. what code did you use?
          • 2. Re: XML Preloader
            Greg Dove Level 4
            I would assume that you need to use setInterval to check the
            getBytesLoaded() vs. getBytesTotal() at regular intervals after the load command has been issued.
            I guess that would work... but I've never tried it... I guess my xml loads have never been that large.

            Its like the old way of doing things for movieclips before the MovieClipLoader class came along. Only one thing... it might give dodgy data in the very early stages of loading. Movieclips do that - I don't know whether it would happen with xml.
            • 3. Re: XML Preloader
              ChrisFlynn Level 1
              Like I said, I've started with the method in the Flash 8 help files:

              // create a new XML document
              var doc:XML = new XML();

              var checkProgress = function(xmlObj:XML) {
              var bytesLoaded:Number = xmlObj.getBytesLoaded();
              var bytesTotal:Number = xmlObj.getBytesTotal();
              var percentLoaded:Number = Math.floor((bytesLoaded / bytesTotal )*100); //<--funny thing here, they left out the * before the 100 in the help file, throws up a syntax error
              trace ("milliseconds elapsed: " + getTimer());
              trace ("bytesLoaded: " + bytesLoaded);
              trace ("bytesTotal: " + bytesTotal);
              trace ("percent loaded: " + percentLoaded);
              trace ("---------------------------------");

              doc.onLoad = function(success:Boolean) {
              trace("intervalID: " + intervalID);
              trace("doc: "+doc);
              doc.load(" http://www.helpexamples.com/flash/xml/ds.xml"); //<--Fairly long, publicly available, XML file
              var intervalID:Number = setInterval(checkProgress, 100, doc);

              The trace results I get when I run this are:

              milliseconds elapsed: 545
              bytesLoaded: 0
              bytesTotal: undefined
              percent loaded: NaN
              milliseconds elapsed: 711
              bytesLoaded: 0
              bytesTotal: undefined
              percent loaded: NaN
              milliseconds elapsed: 877
              bytesLoaded: 0
              bytesTotal: undefined
              percent loaded: NaN
              milliseconds elapsed: 1043
              bytesLoaded: 0
              bytesTotal: undefined
              percent loaded: NaN
              intervalID: 1
              doc = [giant XML file]

              So the bytesLoaded and bytesTotal are always 0 and undefined.

              kglad, how does your method work?

              Thanks again for the help.

              • 4. XML Preloader
                Greg Dove Level 4
                I think it doesn't work in the IDE... off local files. I'm pretty sure Flash loads them fully off local.

                Edit...seems so. Check the comments at the bottom of this. And your missing * has been there for a fair while lol

                • 5. Re: XML Preloader
                  ChrisFlynn Level 1

                  I did see this in my searching. And that's part of the problem. These guys are having the same problems I'm having, but there's no answers here.
                  • 6. Re: XML Preloader
                    kglad Adobe Community Professional & MVP
                    i don't have any problems in the flash test environment or online. here's a test i just ran that looks fine:

                    • 7. Re: XML Preloader
                      ChrisFlynn Level 1
                      This seems to be working for me kglad.

                      You know what I think my problem was, this xml file I point too


                      Only amounts to be 2 k, so a preload for something like is pointless.

                      I just tried your method, and the Flash Help method on a 4 Mb XML file, and they both seem to work.

                      I'll keep at it.

                      Thanks again for the help!
                      • 8. Re: XML Preloader
                        kglad Adobe Community Professional & MVP
                        you're welcome.
                        • 9. XML Preloader
                          Greg Dove Level 4
                          kglad - For me... the IDE loads it fully (unless you simulate a download)... and I believe that's how local files are treated. (Maybe I'm wrong - but I think I've read it in official docs - will check after this post) EDIT: OOPS looks like I am wrong.

                          But the online version (your xml link Chris... which isn't that big - its only around 2k) will for me show one progress before full load if I have a 100 fps frame rate and setInterval just under 10.

                          Have you tried timing the parsing routine in the onData event? Perhaps what you perceive to be the loading delay is small relative to the time it takes flash to parse the xml. If that's the case then I can't suggest any way to measure the progress of that either though...

                          myXML.onData = function(src) {
                          trace('before parsing:'+getTimer());
                          if (src == undefined) {
                          } else {
                          trace('after parsing:'+getTimer());
                          this.loaded = true;
                          • 10. Re: XML Preloader
                            Greg Dove Level 4
                            Sorry - I'm confused (not the first time).

                            Just so I can be sure for my own knowledge - your 4mb test file was definitely a file off the local filesystem?
                            • 11. Re: XML Preloader
                              ChrisFlynn Level 1

                              The 4Mb test was a local file. We're in the process of posting something on a server to test how that loads as well.

                              I have not tried the onData event yet. If this other way turns out to be bust, I'll give it a shot.

                              Thanks again
                              • 12. Re: XML Preloader
                                Greg Dove Level 4
                                OK... well in theory (according to Adobe) it shouldn't work off a local file and the test movie mode (except as per kglad's example with a simulated download).

                                Actually I missed seeing this first time round in the same page on livedocs:

                                "If you attempt to use this example to load a local file that resides on your hard disk, this example will not work properly because in test movie mode Flash Player loads local files in their entirety"

                                However, it also wouldn't be the first time that the help documentation is wrong about something! And 4Mb is a reasonably hefty xml payload - to load in to start with and also I imagine it would take a noticeable chunk of time to parse.

                                • 13. Re: XML Preloader
                                  ChrisFlynn Level 1
                                  Just tested a 10Mb XML from a server with both kglad's and Flash's method. ANd it appears to work.

                                  I really do think the issues before had to do with the 2k size of the XML file I was pointing at.

                                  You can see the progress with a 10Mb file.

                                  Thanks for all the help!