Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9214831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:03:39+00:00 2026-06-18T02:03:39+00:00

I am writing an application in Actionscript 2 that needs to access an XML

  • 0

I am writing an application in Actionscript 2 that needs to access an XML file, get its contents, and then use that information to populate a bunch of text fields. This was straightforward in frame-based actionscript, because all I had to do was stop doing stuff, set the next step to run from the XML.onLoad function and I was good to go. But I’m rewriting this thing with class files and I’m having a hard time getting my XML to load in before the function is called that relies on it being in already.

To elaborate,

Let’s say I have a class, Main. That class looks like:

import XMLItems;

class Main {
    private var _xmlItems:XMLItems;

    public function Main() {
        _xmlItems = new XMLItems();
        _itemData = _xmlItems.getItemData();
}

Looks OK so far, right? All it does is create a new XMLItems instance, and then call that instance’s getItemData() method.

So now here’s XMLItems:

import mx.xpath.XPathAPI;
import XMLLoader;

class XMLItems {

    private var _loader:XMLLoader;
    private var _itemData:XML;
    private var _eventPath:String = "/*/dbresult/playlist/";

    public function XMLItems() {
        trace('XMLItems object instantiated.');
    }

    private function parseItemData(itemData:XML):Array {
        var nodes:Array = XPathAPI.selectNodeList(itemData, _eventPath + "item");

        return nodes;
    }

    public function getItemData():Array {
        _loader = new XMLLoader();
        _itemData = _loader.getXML();
        var r:Array = parseItemData(_itemData);
        return r;
    }

}

So now we have an XMLLoader object to make so that we can call that getXML function (don’t worry, last one):

import mx.xpath.XPathAPI;

class XMLLoader {

    private var _rawXML:XML;
    private var _xmlLocation:String = '';
    private var _recommendRequestURL:String;
    private var _xmlIsLoaded:Boolean = false;

    private var _eventPath:String = "/*/dbresult/playlist/";

    public function XMLLoader() {
        trace('Instantiating XML loader...');
        _recommendRequestURL = _root.recmReqUrl ? _root.recmReqUrl : escape('http://www.myURL.com/');

        _rawXML = new XML();
        _rawXML.ignoreWhite = true;

        var host = this;
        _rawXML.onLoad = function(success:Boolean) {
            if (success) {
                _xmlIsLoaded = true;
                trace('XML data was successfully loaded.');
                var nodes:Array = XPathAPI.selectNodeList(host._rawXML, host._eventPath + "item");
                trace("Number of XML nodes: " + nodes.length);
                trace("Sample node: " + nodes[nodes.length - 1]);
            } else {
                trace('There was an error loading the XML. Please check the XML file.');
            }
        }

        loadXML();
    }

    private function loadXML():Void {
        var xmlLocation:String;
        if ( !_root.recmReqUrl ) {
            trace("There is no network address for the XML file. Defaulting to local settings.")
        xmlLocation = './localBannerData.xml';
        } else {
            xmlLocation = _recommendRequestURL.concat( '&spec=', 'specInfo', 'getCatInfo()', 'getXCatInfo()', '&t=', new Date().getTime() );
        }

        if ( _rawXML.load( xmlLocation ) ) {
            trace('Loading XML file: ' + xmlLocation);
        } else {
                trace('I\'m having difficulty finding the XML. You might want to check your data source.')
        }

    }

    public function getXML():XML {
        return _rawXML;
    }

}

The problem is that when the getXML function is called, the XML hasn’t loaded yet. So the method is returning an empty XML object to the XMLItems class, even though I did this:

_loader = new XMLLoader();
_itemData = _loader.getXML();

which I though meant that the constructor had to finish running before the next statement was evaluated. Apparently it does not.

I’ve tried making a boolean that is set by the onLoad function and putting a while (true) loop in the getXML method (before the method returned) that checks against that boolean and breaks if it returns true, but it never returned true, so it just looped forever.

I really don’t understand what is happening here. Why can’t I get the class to maybe request the XML synchronously and just chill until the XML comes in?

Thanks,
SS

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:03:40+00:00Added an answer on June 18, 2026 at 2:03 am

    Hrm, someone may correct me on this, but I don’t think flash can load external resources (xml or otherwise) synchronously and I believe this may be by design. At least In my experience, whenever external resources are loaded it is always done asynchronously.

    For AS2 XML specifically, it also says load() method calls are asynchronous:

    The load process is asynchronous; it does not finish immediately
    after the load() method is executed.
    http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=Part2_AS2_LangRef_1.html

    You probably already know what this means: you’ll have to add a call back method or have your xml loading class throw some sort of ‘complete’ event you’d listen for (more or less the same thing on the surface for this problem I think).


    added to answer as mentioned in comment

    As mentioned in my comment, you can allow your class to throw custom events by using the EventDispatcher class. I am guessing this class was actually used for dispatching events from flash mx components, which is why it is in the mx package.

    For you need to make your own Event class for event objects. From the docs/googling, (can’t recall where exactly), at the base level an event has two properties: the target (a reference to the object that threw the event) and a name. You can always add more later or extend it if you want for your own custom events.

    class Event {
        public var type:String;
        public var target:Object;
    
        public function Event(inputType:String, inputTarget:Object){
            type = inputType;
            target = inputTarget;
        }
    }
    

    Was double checking here, And it seems you actually don’t need to make your own Event class, it can just be a generic object with these two required properties -but I like to make one anyways to reuse as needed and keep my code cleaner.

    then in your (XML) class:

    // add this
    import mx.events.EventDispatcher;
    
    class MyXMLClass {
    
        public MyXMLClass(){        
            // add this to your constructor
            mx.events.EventDispatcher.initialize(this);
        }
    
        // add these, leave these empty, they are init'ed during runtime
        private function dispatchEvent() {};
        public function addEventListener() {};
        public function removeEventListener() {};
    
    } // end of class
    

    now this adds 3 methods to your class. Within your class methods you can dispatch an event like so:

    // first create the event object
    var event:Event = new Event("complete", this); // 'this' refers to this current object
    dispatchEvent(event);
    

    outside of your class you can register an event listener like so:

    var xmlObject:MyXMLClass = new MyXMLClass();
    
    function eventListenerMethod(inputEvent:Event):Void {
        trace("I got the event: "+ inputEvent.type +" from "+ inputEvent.target);
    }
    xmlObject.addEventListener("complete", eventListenerMethod);
    

    Now, since the event name “complete” is a constant string and should not change, I recommend making it a static constant in the event class like so:

    class Event {
        // added this
        public static var COMPLETE:String = "complete";
    
        public var type:String;
        public var target:Object;
    
        public function Event(inputType:String, inputTarget:Object){
            type = inputType;
            target = inputTarget;
        }
    }
    

    So then you can reference it by going Event.COMPLETE like:

    new Event(Event.COMPLETE, this);
    addEventListener(Event.COMPLETE, eventListenerMethod);
    

    just to reduce the chance of typos and can refer to the events types you have by looking in your event class.

    Now there is 1 more thing to worry about now: I’ve found the event handler method runs in the scope of the object that threw the event. You can verify this by adding trace(this); to the eventListenerMethod() method, and it should trace out your class, Not where that method was written. To solve this, you can use the Delegate class to make that method run in the scope of where it was written.

    so before:

    // assuming this is written in the timeline/frame
    function eventListenerMethod(inputEvent:Event):Void {
        trace("I got the event: "+ inputEvent.type +" from "+ inputEvent.target);
        trace(this); // will trace out "xmlObject" instead of _level0 or _root as you might expect
    }
    xmlObject.addEventListener(Event.COMPLETE, eventListenerMethod);
    

    you can go:

    import mx.utils.Delegate;
    
    function eventListenerMethod(inputEvent:Event):Void {
        trace("I got the event: "+ inputEvent.type +" from "+ inputEvent.target);
        trace(this); // should trace out _level0 / _root now or whatever 'this' was refering to in the Delegate.create() method
    }
    xmlObject.addEventListener(Event.COMPLETE, Delegate.create(this, eventListenerMethod));
    

    Before I was also puzzling over the same issue as you for loading external data and hitting random scope problems and didn’t know how to fix them properly (had to come up with ‘creative’ methods). =b

    Discovering these two things is what allowed me to remain a bit more sane when I had to work in AS2 for a few years and these two things are done a lot better in AS3 (no scope issues/Delegate needed for example, and a lot of objects naturally already extend the EventDispatcher class making it more cleaner to work with or more ‘native’ as I like to say, and it has an actual Event class you can use/extend so you don’t need to make your own).

    Please go and try out the latter half of what I wrote to see if you really need to use the Delegate class though as you may not always need it and can sometimes cause its own problem with garbage collection when you need to remove event listeners (in this case my need to keep an explicit reference to the Delegate object you pass in to addEventListener() so you can pass it in to removeEventListener() later. In my experience I’ve almost always needed it though, which is why I added it to my answer. It can be useful for other things too I’ve found. By itself you can use it to run methods in the scope of other objects (in this case it is used to run the event listener method in the scope of the _root/”where it was written”).

    I highly recommend moving to AS3 when possible. I also recommend looking up the references for EventDispatcher and Delegate classes in the help to get a better idea of what is going on inside -I haven’t had to write any real AS2 for a long time and my memory may be a bit fuzzy. All of what I wrote just now is mostly from memory. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im writing an application that downloads and installs addons for programs which needs to
I'm currently writing a client in ActionScript 3 that talks to a Red5 application/media
I am writing application on ActionScript for Android using Adobe AIR with native extentions.
Im writing an application that supposed to send coordinates in an SMS, but I've
I am writing an application that allows a user to place images on a
I am writing an application that will allow an android phone and java application
I am writing a flex application that involves modifying a textarea very frequently. I
I am writing application that will add usercontrol when user clicks on client area
I am writing application using c++, in windows. I want to get a thumbnail
Problem Description I am writing application for Android and use native code, And test

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.