3 Replies Latest reply on Aug 28, 2007 8:44 AM by Nixy

    how to load XML file inside Object method

    pascal_roobrouck
      Hi,

      I am able to load an XML file but now I want to make my code cleaner and I want to do it inside the constructor of an object.
      Loading an XML file happens in 2 steps :
      1. start the loading by
      myXMLdoc.load("example.xml");
      2. Flash indicates it has finished loading by calling the myXMLdoc.onLoad() function.

      How do I declare/define the onLoad event handler inside a class definition ??
      Here is my class code so far (generating an error around the onLoad function) :

      I am calling the aSiteXMLDoc.load method in the constructor, and I tried to point the aSiteXMLDoc.onLoad handler to the 'this.load' method... but the compiler doesn't like it this way :-(
        • 1. Re: how to load XML file inside Object method
          Nixy Level 1
          Try this

          public function site(aSiteDocFileName:String) {
          var classRef:site = this;

          aSiteXMLDoc = new XML();
          aSiteXMLDoc.ignoreWhite = true;
          aSiteXMLDoc.onLoad = function(success:Boolean):Void { classRef.load(success); };
          aSiteXMLDoc.load(aSiteDocFileName);
          }

          The classRef is only the get a referencve of your Class in the onLoad method.

          If I can suggest you 2 things.
          1- The is a convention for Class definition. All Class start with a upperCase like Myclasses.Site instead of myclasses.site.
          2- The 3 other methods in your Class can be private instead of public. This will keep your code in that class only.

          It's just suggestion. No obligations.
          Hope that can help
          • 2. Re: how to load XML file inside Object method
            pascal_roobrouck Level 1
            Thanks Nixy for your to the point answer as well as 2 usefull suggestions. I'm new to actionscript and OO, so any good tips are welcome.

            I am still puzzled why the extra 'classRef' var is needed, as the code seems to compile also with just using 'this' instead, but I'll play with it a little more to find out.

            Bye ;-)
            • 3. Re: how to load XML file inside Object method
              Nixy Level 1
              Your're welcome
              Yes, in your case the this keyword will works. But you'll face other situations that the this will not works and you'll have to use the way I suggest you. I don't remember exactly those situations, but I remember that I had that problem and the Flash Help was suggesting the other way. If I found where I'll tell you.

              Well you can use the this, and if you face off the problem you'll be abled to fix it. But you can also start working with the var classRef:site = this; and all your code will be similar.

              Note that the classRef:site can be any other words. I call it classRef, but it could be watever you want. Just need to be the type of your Class, in your case site, and equal to this;

              Well it's nice that you start working in OOP.