1

I need to parse an XML like this:

<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<pictures>

    <pic>
        <name>clouds1</name>
        <file>clouds1.jpg</file>
        <date>20/12/09</date>
    </pic>

    <pic>..........</pic>
    ....

</pictures> 

using ActionScript.

I have this:

constructor{
    var loader = new URLLoader(new URLRequest("data.xml"));
    loader.addEventListener(Event.COMPLETE, loadedCompleteHandler);

    //code that need the arrays created on the function below. This code cannot be in the function below
}

private function loadedCompleteHandler(e:Event):void 
{
    e.target.removeEventListener(Event.COMPLETE, loadedCompleteHandler);
    _xml = XML(e.target.data);

    for( var i:int = 0 ; i <= _xml.object.length() ; i++ ){
        var object:XML = _xml.object[i];
        nameArray[i] = object.name;
        fileArray[i] = object.file;
        dateArray[i] = object.date;
    }
}

but I want to wait until the xml is fully loaded. It seems that when I call it in the constructor, the program makes a "thread" and continues the execution with the arrays to null because it needs more time to load. I need to run all in the constructor.

Thank you very much

2 Answers 2

1

If you're working in an Adobe AIR environment with local xml files, you can use the FileStream class to make your xml-file loading synchronous.

var file:File = File.applicationDirectory.resolvePath("myFile.xml");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
var rawData:String = fileStream.readUTFBytes(fileStream.bytesAvailable);
fileStream.close();
var out:XML = XML(rawData);
// XML parsing stuff here
Sign up to request clarification or add additional context in comments.

Comments

0

No threads are made.
Actionscript is single threaded*.

The xml will start to load and the function loadedCompleteHandler() will run once the file is completely loaded. There is no way to pause execution (ie making a synchronous request).

What you need to do is make your loadedCompleteHandler call another function that continues what you started in your constructor. Another option is to either load the xml before even calling the constructor or embedding the xml inside the swf.

* there are exceptions, but let's skip them for simplicity

2 Comments

and how can i load the XML before calling the constructor?
you can't... you need to move your initialization routine to another function and call that after you load and parse the XML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.