View comments | RSS feed

load (XML.load method)

public load(url:String) : Boolean

Loads an XML document from the specified URL, and replaces the contents of the specified XML object with the downloaded XML data. The URL is relative and is called using HTTP. The load process is asynchronous; it does not finish immediately after the load() method is executed.

When the load() method is executed, the XML object property loaded is set to false. When the XML data finishes downloading, the loaded property is set to true, and the onLoad event handler is invoked. The XML data is not parsed until it is completely downloaded. If the XML object previously contained any XML trees, they are discarded.

You can define a custom function that executes when the onLoad event handler of the XML object is invoked.

Note: If a file being loaded contains non-ASCII characters (as found in many non-English languages), it is recommended that you save the file with UTF-8 or UTF-16 encoding as opposed to a non-Unicode format like ASCII.

When using this method, consider the Flash Player security model:

For Flash Player 8:

For more information, see the following:

For Flash Player 7 and later websites can permit cross-domain access to a resource via a cross-domain policy file. In SWF files of any version running in Flash Player 7 and later, the url parameter must be in exactly the same domain. For example, a SWF file at www.someDomain.com can load data only from sources that are also at www.someDomain.com.

In SWF files running in a version of the player earlier than Flash Player 7, the url parameter must be in the same superdomain as the SWF file that issues this call. A superdomain is derived by removing the leftmost component of a file's URL. For example, a SWF file at www.someDomain.com can load data from sources at store.someDomain.com, because both files are in the same superdomain of someDomain.com.

Availability: ActionScript 1.0; Flash Player 5 - The behavior changed in Flash Player 7.

Parameters

url:String - A string that represents the URL where the XML document to be loaded is located. If the SWF file that issues this call is running in a web browser, url must be in the same domain as the SWF file.

Returns

Boolean - A Boolean value of false if no parameter (null) is passed; true otherwise. Use the onLoad() event handler to check the success of a loaded XML document.

Example

The following code example uses the XML.load() method:

// Create a new XML object.
var flooring:XML = new XML();

// Set the ignoreWhite property to true (default value is false).
flooring.ignoreWhite = true;

// After loading is complete, trace the XML object.
flooring.onLoad = function(success) {
    trace(flooring);
};

// Load the XML into the flooring object.
flooring.load("flooring.xml");

For the contents of the flooring.xml file, and the output that this example produces, see the example for the XML.ignoreWhite property.

See also

ignoreWhite (XML.ignoreWhite property), loaded (XML.loaded property), onLoad (XML.onLoad handler), useCodepage (System.useCodepage property)


Version 8

Comments


x-crow said on Oct 7, 2005 at 11:25 AM :
Found a bug where xml.load(www.myDomain.com) works fine but xml.load(subhost.myDomain.com) doesn't work... it needs to be www for the host name which really sucks, is there a hack for this?
No screen name said on Nov 15, 2005 at 7:53 AM :
In case you haven’t found a solution to the subdomain issue:
There are a couple of ways around this problem as I use dynamic subdomains on several of my sites. One thing you can do is to create a base tag in the header of your html document and set the href attribute to the full domain of the scripts or movies that your swf file needs to use.
The other is to pass the domain to the flash file in a query string or javascript and then add or update the “System.security.allowDomain” with that data (see below for a quick example).

In the object/embed tags of your html document append the URL calling the flash file as follows:

“exampleflash.swf?scriptAccessURL=www.mydomain.com”.

Then create the following line of code in your movie before you call any network files or scripts:

System.security.allowDomain(_root.scriptAccessURL);

If you don’t normally use fully qualified domain names in your html pages the second suggestion may be a better solution.
Thais Derich said on Nov 15, 2005 at 1:48 PM :
x-crow,
Thank you for your comment.
The functionality that you described in your comment is not a bug but a security feature.
I've pasted the relevant paragraph from the above documentation here:

For Flash Player 7 and later websites can permit cross-domain access to a resource via a cross-domain policy file. In SWF files of any version running in Flash Player 7 and later, the url parameter must be in exactly the same domain. For example, a SWF file at www.someDomain.com can load data only from sources that are also at www.someDomain.com.
SMakinson said on Feb 21, 2006 at 8:11 AM :
I am working with Flash 8 and I may have a found a bug with the security
model. Please correct me if I'm wrong, but here is what's happening:

All files are on the SAME DOMAIN and it works locally. I am unable to
load xml when on the server unless I use the fully qualified path that
includes the exact domain in the browser address bar. http://
www.mysite.com/xml/file.xml. Relative paths and paths based on the site
root do not even attempt to load as far as I can tell. Even if I use http://
mysite.com/xml/file.xml ( Notice the www. is gone ) when I had typed the
www in the address bar it fails. I have never had these issues before.
Flash should be able to tell its supposed to use the same domain,
correct?

I have tired using System.security.allowDomain("*") with no change. I
have just for the heck of it tried making a crossdomain.xml file with no
change. It didn't appear to be looking for that anyway in the Safari activity
window. So for now I am forced to go ahead and dynamically find the
domain myself to use in the load path. Please someone tell me this isn't
the way its supposed to be in 8.

Thanks.
SMakinson said on Feb 28, 2006 at 6:36 PM :
Scratch my previous comment. It turned out the for some reason the cache killer code in the class I was using was causing the url to not load on the server. Once I changed that a bit all is fine.
BanzaisBandExperience said on Aug 25, 2006 at 5:07 AM :
Working on flash-game, which comunicates with server-script (it returns an XML-file with data), i've noticed one thing - When I start animation and load XML at the same moment - I see a delay in animation. It happens when I run swf in the browser - there is no delay if I'm in debug mode.

By the way - I saw the same behaviour in LoadVars, when flash-movie was converted to projector-file.

How can this be explained - does flash player (standalone or plugin) stop animation during access to a server?
Paul Feakins said on Sep 28, 2006 at 6:09 AM :
I'm getting the same problem as SMakinson; when you use the Absolute URL to an XML file it works fine, but a relative path refuses to load it, even when the swf and the xml file are in the same directory on the server. Of course, it works locally.

The swf loads the xml from a relative URL when you browse to it directly, but it is loaded in to another swf itself, and it is when your browse to its parent swf file, it cannot load the xml in.
marcusw1234 said on Oct 18, 2006 at 6:08 AM :
I have stumbled across a problem when loading XML via an external address in Firefox.

If you specify a valid port like 'http://www.mydomain.com:80/test.xml' in the path for XML.load(), then it will not work when viewing the Flash in Firefox It works correctly in IE and also in both browsers when viewing the page locally (rather than from a server).
ecukitman said on Apr 7, 2007 at 9:10 PM :
How come the swf file from 'boot drive\Program Files\Macromedia\Flash 8\Samples and Tutorials\Samples\ActionScript\XML_BlogTracker' can load XML file from different domain? (load xml from http://weblogs.macromedia.com/flashteam/index.rdf from http://localhost/xml_blogTracker.swf)
VanTon said on Jun 15, 2007 at 5:32 PM :
I wonder if I can use http compression aka Gzip to compress the xml file which is dynamic created by web server. Can flash decompress and use it or actually the brower decompress it and flash use it.
No screen name said on Jun 18, 2007 at 7:41 AM :
The same happens with me, and worst, when I make a small script just to see if I can load a XML document from my website (after publishing the file) it doesn´t open the XML file hosted in my site (I'm using the complete address with the http:// and www). But when I change the link to my file for the link to the XML of that example it opens the example's XML. Why doesn't it open my XML on my site since both of them (My XML and the examples XML are online)??
toola said on Jul 26, 2007 at 6:39 PM :
I have heard some talk of a timeout built into the XML.load method.

If there is one it should be mentioned here in the docs, along with what the behavior / return value is when it is reached.

 

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

Current page: http://livedocs.adobe.com/flash/8/main/00002872.html