1 Reply Latest reply on Feb 20, 2007 12:55 AM by Peter Lorent

    XML class instance problems while getting result

    Lujunq Level 1
      Hi, I'm trying to use a XML object in a class I'm creating. Something like this:

      class MyClass {
      // vars
      private var myXML:XML;
      private var myString:String;

      // constructor
      function MyClass(url:String) {
      this.myXML = new XML();
      this.myXML.ignoreWhite = true;
      this.myXML.onLoad = function(success) {
      trace (this.myXML);
      };
      }
      }

      However this seems not work. The problem is that I'm never able to get the content of a XML object after loading it if it is declared as a class variable (after loading, myXML always get "undefined"). If I change the function above to a variable local only to the function it works perfectly:

      // constructor
      function MyClass(url:String) {
      var myXML:XML = new XML();
      myXML.ignoreWhite = true;
      myXML.onLoad = function(success) {
      trace (myXML);
      };
      }

      But, by doing this I'm not able to pass the XML result to any other class variable, since the word "this", inside the "onLoad" function will point to the XML itself and not to my class, so, including

      this.myString = myXML;

      would never work since there is no way to access the class variable myString inside the "onLoad" function.

      Is there a way to access the XML result and maybe save it to another variable inside my class?
        • 1. Re: XML class instance problems while getting result
          Peter Lorent Level 2
          function MyClass(url:String) {
          this.myXML = new XML();
          this.myXML.ignoreWhite = true;
          this.myXML.onLoad = function(success) {
          trace (this); -> inside the callback handler 'this' refers to the myXML object
          };
          }
          }

          So, why trace(this.myXML); won't work? As commented above, 'this' inside the callback handler refers to the myXML object. And inside myXML there is no object myXML (as a child..).