Skip to main content
Known Participant
September 19, 2010
Answered

Accessing XML data from a different class

  • September 19, 2010
  • 3 replies
  • 1062 views

Hi all,

I have an xml class that loads xml data, I need to access the data from another class. I have imported the xml classinto the new class and created a new instance of it. However when I try to access the xml data it is coming back as null. I understand this is almost certainly because when it is called the xml data hasn't completed it's load. How can I get round this?

xml class:

package {
   
    import flash.xml.*;
    import flash.events.*;
    import flash.net.*
    import flash.display.*
   
    public class xml extends MovieClip
    {
        public var xmlRequest:URLRequest;
        public var xmlLoader:URLLoader;
        public var xmlImages:XML;
       
        public function xml()
        {
            xmlRequest = new URLRequest("images.xml");
            xmlLoader = new URLLoader(xmlRequest)
           
            xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
            xmlLoader.load(xmlRequest);
        }
       
        private function xmlLoaded(event:Event):void
        {
            trace(xmlLoader.data);
            xmlImages = new XML(xmlLoader.data);
        }
    }
}

Thanks in advance

This topic has been closed for replies.
Correct answer Andrei1-bKoviI

Or better yet:

package {
     import flash.events.*;
     import flash.net.*;
     import flash.xml.*;
  
     public class XMLLoader extends URLLoader
     {
          public var xmlImages:XML;
      
          public function XMLLoader()
          {
              
          }
          
          public function loadXML(url:String):void {
               this.addEventListener(Event.COMPLETE, xmlLoaded);
               this.load(new URLRequest(url));
          }
      
          private function xmlLoaded(event:Event):void
          {
               trace(xmlLoader.data);
               xmlImages = newXML(this.data);
               dispatchEvent(new Event("loadComplete"));
          }
     }
}

Usage:

var xmlLoader:XMLLoader = new XMLLoader();
xmlLoader.addEventListener("loadComplete", onXMLLoad);

xmlLoader.loadXML("images.xml");

function onXMLLoad(e:Event):void {
    trace(xmlLoader.xmlImages);
}

3 replies

Participant
September 19, 2010

It's a helpful for us. You can know also from where London web design

Andrei1-bKoviICorrect answer
Inspiring
September 19, 2010

Or better yet:

package {
     import flash.events.*;
     import flash.net.*;
     import flash.xml.*;
  
     public class XMLLoader extends URLLoader
     {
          public var xmlImages:XML;
      
          public function XMLLoader()
          {
              
          }
          
          public function loadXML(url:String):void {
               this.addEventListener(Event.COMPLETE, xmlLoaded);
               this.load(new URLRequest(url));
          }
      
          private function xmlLoaded(event:Event):void
          {
               trace(xmlLoader.data);
               xmlImages = newXML(this.data);
               dispatchEvent(new Event("loadComplete"));
          }
     }
}

Usage:

var xmlLoader:XMLLoader = new XMLLoader();
xmlLoader.addEventListener("loadComplete", onXMLLoad);

xmlLoader.loadXML("images.xml");

function onXMLLoad(e:Event):void {
    trace(xmlLoader.xmlImages);
}

thahipAuthor
Known Participant
September 19, 2010

Thank you so much Andrei! That worked a treat

Inspiring
September 19, 2010

You are welcome.


Inspiring
September 19, 2010

One of the ways:

package {
     import flash.xml.*;
    import flash.events.*;
    import flash.net.*
    import flash.display.*
  
    public class XMLLoader extends EventDispatcher
    {
          public var xmlRequest:URLRequest;
          public var xmlLoader:URLLoader;
          public var xmlImages:XML;
      
          public function XMLLoader()
          {
               
          }
          
          public function loadXML(url:String):void {
               xmlLoader = new URLLoader()
               xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
               xmlLoader.load(new URLRequest(url));
          }
      
          private function xmlLoaded(event:Event):void
          {
               trace(xmlLoader.data);
               xmlImages = new XML(xmlLoader.data);
               dispatchEvent(event);
          }
    }
}

Usage:

var xmlLoader:XMLLoader = new XMLLoader();

xmlLoader.addEventListener(Event.COMPLETE, onXMLLoad);

xmlLoader.loadXML("images.xml"):

function onXMLLoad(e:Event):void{

     trace(xmlLoader.xmlImages);

}