7 Replies Latest reply on May 27, 2006 4:45 AM by tomaxnol

    XML Class

    tomaxnol
      Hi, I've been looking up and down the forums and the web to find a solution, albeit to no avail.

      Basically I created a simple XML Class which converts XML data into an associative array, which I then want to send back to the calling Flash file in order to do something with the parsed data. However, for some reason, when i want to access this array from the calling Flash file I always get an 'undefined' message.

      This is how I try to access the returnVal of the class from outside:

      var xml : GetXML = new GetXML('some.xml');
      var assocArray : Object = xml.obj;
      trace(assocArray); // returns 'undefined'

      Like I said, I am getting an 'undefined', almost as if the call to load that object happens before it is actually compiled within the class. The funny thing though is, that the info shows up correctly in the debugger window when i call 'List Variables'. It is also traceable when I call the object from within the class. Here is the class code
        • 1. Re: XML Class
          tomaxnol
          actually there is a line missing in the class code
          private var returnObj : Object;

          but it's in the original class, so this is not the error.
          • 2. Re: XML Class
            Level 7
            You have to wait for the xml to be loaded and "decoded" before getting
            xml.obj.


            "tomaxnol" <webforumsuser@macromedia.com> wrote in message
            news:e57968$r4f$1@forums.macromedia.com...
            > Hi, I've been looking up and down the forums and the web to find a
            solution,
            > albeit to no avail.
            >
            > Basically I created a simple XML Class which converts XML data into an
            > associative array, which I then want to send back to the calling Flash
            file in
            > order to do something with the parsed data. However, for some reason, when
            i
            > want to access this array from the calling Flash file I always get an
            > 'undefined' message.
            >
            > This is how I try to access the returnVal of the class from outside:
            >
            > var xml : GetXML = new GetXML('some.xml');
            > var assocArray : Object = xml.obj;
            > trace(assocArray); // returns 'undefined'
            >
            > Like I said, I am getting an 'undefined', almost as if the call to load
            that
            > object happens before it is actually compiled within the class. The funny
            > thing though is, that the info shows up correctly in the debugger window
            when i
            > call 'List Variables'. It is also traceable when I call the object from
            within
            > the class. Here is the class code
            >
            > import mx.utils.Delegate;
            >
            > class GetXML {
            > private var xmlData : XML;
            > private var urlstring : String;
            > private var firstChild : XMLNode;
            >
            > // getters and setters
            > public function set obj(a : Object) : Void { returnObj = a; }
            > public function get obj() { return returnObj; }
            >
            > public function GetXML(urlstring:String) {
            > this.urlstring = urlstring;
            > this.xmlData = new XML();
            > this.xmlData.ignoreWhite = true;
            > this.xmlData.contentType = "text/xml";
            > this.xmlData.onLoad = Delegate.create(this, decode);
            > this.xmlData.load(this.urlstring);
            > }
            >
            > private function decode(success:Boolean):Void{
            > if(success) {
            > var xpath : Array = this.xmlData.firstChild.firstChild.childNodes;
            > var xdata = {};
            > xdata.info = new Array();
            > xdata.media = new Array();
            >
            > for (i = 0; i < xpath.length; i++){
            > if(xpath .firstChild.nodeValue != null){
            > xdata.info.push(xpath
            .firstChild.nodeValue);
            > }else{
            > for(j = 0; j < xpath .childNodes.length; j++) {
            > if(xpath
            .childNodes[j].firstChild.nodeValue != null){
            > xdata.media.push(xpath .childNodes[j].firstChild.nodeValue);
            > }
            > }
            > }
            > this.obj = xdata;
            > }
            > }
            > }
            > }
            >


            • 3. Re: XML Class
              Level 7
              Something like this should work.

              class GetXML {
              public onready;

              // other methods etc..


              private function decode(success:Boolean):Void{
              // your code

              if (this.onready != undefined){this.onready();}
              }

              }

              var xml : GetXML = new GetXML('some.xml');
              xml.onready = function(){
              var assocArray : Object = xml.obj;
              trace(assocArray); // returns 'undefined'
              }




              "Raymond Basque" <NOSPAMrbasque@jednm.comNOSPAM> wrote in message
              news:e579nc$rru$1@forums.macromedia.com...
              > You have to wait for the xml to be loaded and "decoded" before getting
              > xml.obj.
              >
              >
              > "tomaxnol" <webforumsuser@macromedia.com> wrote in message
              > news:e57968$r4f$1@forums.macromedia.com...
              > > Hi, I've been looking up and down the forums and the web to find a
              > solution,
              > > albeit to no avail.
              > >
              > > Basically I created a simple XML Class which converts XML data into an
              > > associative array, which I then want to send back to the calling Flash
              > file in
              > > order to do something with the parsed data. However, for some reason,
              when
              > i
              > > want to access this array from the calling Flash file I always get an
              > > 'undefined' message.
              > >
              > > This is how I try to access the returnVal of the class from outside:
              > >
              > > var xml : GetXML = new GetXML('some.xml');
              > > var assocArray : Object = xml.obj;
              > > trace(assocArray); // returns 'undefined'
              > >
              > > Like I said, I am getting an 'undefined', almost as if the call to load
              > that
              > > object happens before it is actually compiled within the class. The
              funny
              > > thing though is, that the info shows up correctly in the debugger window
              > when i
              > > call 'List Variables'. It is also traceable when I call the object from
              > within
              > > the class. Here is the class code
              > >
              > > import mx.utils.Delegate;
              > >
              > > class GetXML {
              > > private var xmlData : XML;
              > > private var urlstring : String;
              > > private var firstChild : XMLNode;
              > >
              > > // getters and setters
              > > public function set obj(a : Object) : Void { returnObj = a; }
              > > public function get obj() { return returnObj; }
              > >
              > > public function GetXML(urlstring:String) {
              > > this.urlstring = urlstring;
              > > this.xmlData = new XML();
              > > this.xmlData.ignoreWhite = true;
              > > this.xmlData.contentType = "text/xml";
              > > this.xmlData.onLoad = Delegate.create(this, decode);
              > > this.xmlData.load(this.urlstring);
              > > }
              > >
              > > private function decode(success:Boolean):Void{
              > > if(success) {
              > > var xpath : Array = this.xmlData.firstChild.firstChild.childNodes;
              > > var xdata = {};
              > > xdata.info = new Array();
              > > xdata.media = new Array();
              > >
              > > for (i = 0; i < xpath.length; i++){
              > > if(xpath .firstChild.nodeValue != null){
              > > xdata.info.push(xpath
              .firstChild.nodeValue);
              > > }else{
              > > for(j = 0; j < xpath .childNodes.length; j++) {
              > > if(xpath
              .childNodes[j].firstChild.nodeValue != null){
              > > xdata.media.push(xpath .childNodes[j].firstChild.nodeValue);
              > > }
              > > }
              > > }
              > > this.obj = xdata;
              > > }
              > > }
              > > }
              > > }
              > >
              >
              >


              • 4. Re: XML Class
                Level 7
                Oops!

                Should be

                public var onready;

                "Raymond Basque" <NOSPAMrbasque@jednm.comNOSPAM> wrote in message
                news:e57a2c$sdq$1@forums.macromedia.com...
                > Something like this should work.
                >
                > class GetXML {
                > public onready;
                >
                > // other methods etc..
                >
                >
                > private function decode(success:Boolean):Void{
                > // your code
                >
                > if (this.onready != undefined){this.onready();}
                > }
                >
                > }
                >
                > var xml : GetXML = new GetXML('some.xml');
                > xml.onready = function(){
                > var assocArray : Object = xml.obj;
                > trace(assocArray); // returns 'undefined'
                > }
                >
                >
                >
                >
                > "Raymond Basque" <NOSPAMrbasque@jednm.comNOSPAM> wrote in message
                > news:e579nc$rru$1@forums.macromedia.com...
                > > You have to wait for the xml to be loaded and "decoded" before getting
                > > xml.obj.
                > >
                > >
                > > "tomaxnol" <webforumsuser@macromedia.com> wrote in message
                > > news:e57968$r4f$1@forums.macromedia.com...
                > > > Hi, I've been looking up and down the forums and the web to find a
                > > solution,
                > > > albeit to no avail.
                > > >
                > > > Basically I created a simple XML Class which converts XML data into
                an
                > > > associative array, which I then want to send back to the calling Flash
                > > file in
                > > > order to do something with the parsed data. However, for some reason,
                > when
                > > i
                > > > want to access this array from the calling Flash file I always get an
                > > > 'undefined' message.
                > > >
                > > > This is how I try to access the returnVal of the class from outside:
                > > >
                > > > var xml : GetXML = new GetXML('some.xml');
                > > > var assocArray : Object = xml.obj;
                > > > trace(assocArray); // returns 'undefined'
                > > >
                > > > Like I said, I am getting an 'undefined', almost as if the call to
                load
                > > that
                > > > object happens before it is actually compiled within the class. The
                > funny
                > > > thing though is, that the info shows up correctly in the debugger
                window
                > > when i
                > > > call 'List Variables'. It is also traceable when I call the object
                from
                > > within
                > > > the class. Here is the class code
                > > >
                > > > import mx.utils.Delegate;
                > > >
                > > > class GetXML {
                > > > private var xmlData : XML;
                > > > private var urlstring : String;
                > > > private var firstChild : XMLNode;
                > > >
                > > > // getters and setters
                > > > public function set obj(a : Object) : Void { returnObj = a; }
                > > > public function get obj() { return returnObj; }
                > > >
                > > > public function GetXML(urlstring:String) {
                > > > this.urlstring = urlstring;
                > > > this.xmlData = new XML();
                > > > this.xmlData.ignoreWhite = true;
                > > > this.xmlData.contentType = "text/xml";
                > > > this.xmlData.onLoad = Delegate.create(this, decode);
                > > > this.xmlData.load(this.urlstring);
                > > > }
                > > >
                > > > private function decode(success:Boolean):Void{
                > > > if(success) {
                > > > var xpath : Array = this.xmlData.firstChild.firstChild.childNodes;
                > > > var xdata = {};
                > > > xdata.info = new Array();
                > > > xdata.media = new Array();
                > > >
                > > > for (i = 0; i < xpath.length; i++){
                > > > if(xpath .firstChild.nodeValue != null){
                > > > xdata.info.push(xpath
                .firstChild.nodeValue);
                > > > }else{
                > > > for(j = 0; j < xpath .childNodes.length; j++) {
                > > > if(xpath
                .childNodes[j].firstChild.nodeValue != null){
                > > > xdata.media.push(xpath .childNodes[j].firstChild.nodeValue);
                > > > }
                > > > }
                > > > }
                > > > this.obj = xdata;
                > > > }
                > > > }
                > > > }
                > > > }
                > > >
                > >
                > >
                >
                >


                • 5. Re: XML Class
                  tomaxnol
                  Thanks Raymond.

                  It actually works when I employ an onEnterFrame function in the flash file which tracks whether an 'isLoaded' variable in the class is set to true or not. The data shows up the second time the frame is entered. But that seems like a really ugly hack.
                  • 6. Re: XML Class
                    TimSymons Level 1
                    You should add a Event broadcast in the class so that you could use a listener instead.

                    • 7. XML Class
                      tomaxnol
                      Thanks Tim,

                      I thought about that, but event listeners kind of scare me ;-). i'll give it a try, though, because this enterFrame thing seems like a really bad workaround (even though it works).

                      Edit: worked.... thanks a lot. you're the man!