View comments | RSS feed

Reading external XML documents

You can use the URLLoader class to load XML data from a URL. To use the following code in your applications, replace the XML_URL value in the example with a valid URL:

var myXML:XML = new XML();
var XML_URL:String = "http://www.example.com/Sample3.xml";
var myXMLURL:URLRequest = new URLRequest(XML_URL);
var myLoader:URLLoader = new URLLoader(myXMLURL);
myLoader.addEventListener("complete", xmlLoaded);

function xmlLoaded(event:Event):void
{
    myXML = XML(myLoader.data);
    trace("Data loaded.");
}

You can also use the XMLSocket class to set up an asynchronous XML socket connection with a server. For more information, see the ActionScript 3.0 Language and Components Reference.


Flash CS3


Comments


RavenWorks said on May 27, 2007 at 2:10 PM :
Is it bad practice to add the event listener AFTER starting the load like that? This script seems to depend on the load not finishing before the listener is added. Is there a guaranteed minimum amount of time that any load is going to take before firing an event, even on a 0 byte file?
MotionMaker said on Jun 20, 2007 at 10:15 AM :
Variables are undefined in xmlLoaded

Change

myXML = XML(myLoader.data);

to

var myXML:XML = XML(event.target.data);
MotionMaker said on Jun 20, 2007 at 10:24 AM :
The example of a "statement" function versus and "expression" function
allows using it before it appears in code. I can be a less acceptable style
practice . However the code sequence is ok.

Expression function:
expressionTest(); // run-time error

var expressionTest:Function = function ()
{
trace("expressionTest");
}
Statement function example:
statementTest(); // statementTest in trace

function statementTest():void
{
trace("statementTest");
}
adbe_paul said on Jun 26, 2007 at 4:13 PM :
@RavenWorks:

It's not bad practice in the sense that it always will work. Some developers find it clearer to add the event listeners before calling the operation which results in the events, while other developers prefer to group all the addEventListener() calls at the end to separate them.

In any case, because of the way asynchronous operations (like loading an XML file) work in ActionScript, it wouldn't be possible for the event to be dispatched before the addEventListener() call happens. The reason is because ActionScript is single-threaded, meaning there is always only one set of ActionScript instructions being executed at one time. An entire set of instructions will always be run in sequence before any other sets of instructions (such as event listeners) are called. (In fact, after something like load() is called, the event listeners are never called until at least the next "frame" of time.) So any time you're calling an asynchronous operation, as long as you add your event listeners within the same set of code (e.g. in the same function block or in the same timeline script) your listeners will always be added before the events are dispatched.
adbe_paul said on Jun 27, 2007 at 12:33 PM :
@MotionMaker:
Re: "Variables are undefined..."

You noted that "Variables are undefined in xmlLoaded", which isn't necessarily true (but can be true). Presumably, you're putting the non-function code (i.e. the lines from "var myXML:XML = new XML(); to myLoader.addEventListener()") in a separate function, in which case the variable myLoader wouldn't be available in the scope of the xmlLoaded function. However, as it's written, if for example you paste the code into a frame script in Flash, or if you declare myLoader as a member variable in a class and wrap the rest of the code in a function, it works fine and the variables aren't undefined.

As the code is written, the two versions are exactly equivalent -- since myLoader is the object that's dispatching the event, myLoader.data is exactly the same as event.target.data. If you do have access to the object that dispatches the event through a different variable (as in this case with myLoader) then it can be slightly better to do it that way because 1) you can get code hints while you're writing your code and 2) at run time, Flash Player doesn't have to convert the object to a different data type, like it does with event.target (since event.target's data type is Object) so using the myLoader version will have a small performance benefit. However, if you *don't* have access to the object that's dispatching the event through another variable, then you can use the event.target version to achieve the same result.
No screen name said on Jul 2, 2007 at 3:06 AM :
Hi.
I would like to know if there is a way to reload an XML when it changes. For example at the beginning of my flash movie i load an XML in which i have information how to draw my screen. So, for as long as my XML stays the same, my screen stays the same, but when my XML changes, i would like to redraw my screen. Any way to do this?
Thank you for help.
danwize said on Sep 6, 2007 at 4:21 PM :
I have tried using the example code above to read an xml file but I keep getting error 1088: The markup in the document following the root element must be well-formed.
I'd like to see an example of the form it should be in. I have successfully parsed the same xml using actionscript 2.0. Here is a sample of the xml I've been trying to parse:

<?xml version="1.0" encoding="UTF-16"?>
<basePath basePath="lib/"/>

<word0
english="I didn't understand very well"
spanish="no entendí bien"
image="imgs/iDidntUnderstandVeryWell.swf"
engAudio="engAudio/iDidntUnderstandVeryWell.mp3"
spanAudio="spanAudio/noEntendiBien.mp3"
/>

<word0
english="What does _______ mean?"
spanish="qué significa "
image="imgs/whatDoes___Mean.swf"
engAudio="engAudio/whatDoes-Mean.mp3"
spanAudio="spanAudio/queSignifica.mp3"
/>
No screen name said on Jan 25, 2008 at 11:21 PM :
using xml its not good practice to use single data notations as you have for instance

<word0
english="I didn't understand very well"
spanish="no entendí bien"
image="imgs/iDidntUnderstandVeryWell.swf"
engAudio="engAudio/iDidntUnderstandVeryWell.mp3"
spanAudio="spanAudio/noEntendiBien.mp3"
/>

should be:

<word0>
<english>I didn't understand very well" </english>
<spanish>no entendí bien" </spanish>
<image>imgs/iDidntUnderstandVeryWell.swf" </image>
<engAudio>engAudio/iDidntUnderstandVeryWell.mp3" </engAudio>
<spanAudio>spanAudio/noEntendiBien.mp3" </spanAudio>
</word0

you see when you use a format like:
<Main color="red" ></Main>

it uses single data where something like this:
<Main>
<color>red</color>
</Main

then you can add multiple attributes with out hurting the layout of the structure... sure it will work both ways but its just good practice to do the way displayed...

i've always told myself that if i'm gonna learn i'm gonna learn the right way.

good luck on your voyage into as3...
princezoe23 said on Jun 24, 2008 at 9:12 AM :
in the xml address can I send some url variable with that address using the

address?variable routine
djtechwriter said on Jul 10, 2008 at 11:45 AM :
princezoe23,

Yes. See the flash.net.URLRequest.url property for information on formatting URL variables. Or, the flash.net.URLVariables class to create an object containing the variables to pass to the URLLoader.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000132.html