Flash cross-domain XML fetching

If you need to fetch data within Flash which resides on another domain, you will find out it will not work once you test it outside Flash.exe. XML.load(), LoadVars() and sendAndLoad() will return false because of cross-domain security issues.

Where the cross-domain file did not do any good, I found another solution to fetch cross-domain XML data. The so-called 'shim' file.

One important thing: this solution is for the situation when you have control of the server that supplies the (XML) data to the main Flash file on a server out of your control.

The shim file is basically a second movie, hosted on the same server as the data source. The main movie loads the shim movie using loadMovie or the data with loadMovieNum, so both movies are loaded into the Macromedia Flash Player at the same time (on different levels).

To create a shim file just create a new Flash file. In the first frame put the following ActionScript:

var loaded = false;
var varXML = new XML();
varXML.ignoreWhite = true;
varXML.load("http://www.yourdatadomain.com/yourxmlfile.xml");
varXML.onLoad = function(success) { if(success) loaded = true; }
System.security.allowDomain("http://www.yourflashdomain.com/");

Because both documents are in the same domain, the XML request will be processed. The shim file size is only around 250 bytes, so it won't take up much bandwidth.

While loading cross-domain data will trigger security issues, loading a SWF file will not. Hence the next step: load the shim file into your main movie. In your main movie's actionscript add the following:

loadMovieNum("http://www.yourdatadomain.com/shim.swf", 1);
var varXML = new XML();
this.onEnterFrame = function() {
    if(_level1.loaded) {
        delete this.onEnterFrame;
        varXML.parseXML(_level1.varXML);
        // Do something with the XML //
    }
}

Because you do not know when the data finished loading, I added an onEnterFrame function which keeps checking the status. If the XML data has loaded, parse it into the _root level and go wild with it!

Author rcz Time Sep 9, 2007 Comment on 'Flash cross-domain XML fetching' Add comment Bookmark 'Flash cross-domain XML fetching' Bookmark Permalink to 'Flash cross-domain XML fetching' Permalink
Bookmark 'Flash cross-domain XML fetching' to Netvouz Bookmark 'Flash cross-domain XML fetching' to Facebook Bookmark 'Flash cross-domain XML fetching' to Furl Bookmark 'Flash cross-domain XML fetching' to Technorati Bookmark 'Flash cross-domain XML fetching' to Stumble Upon Bookmark 'Flash cross-domain XML fetching' to Digg Bookmark 'Flash cross-domain XML fetching' to Del.icio.us

Comments (4)

Comment author Page Turning Software Comment author email desoja.jain<at>gmail.com Time Apr 2, 2009 01:05 GMT #1
If you circulate a publication then you want to know what your readers think and how they interact with your printed <a href=http://www.pageturnpro.com>Digital publication</a>. Unfortunately, print surveys for these publications are costly, time consuming and very rarely are they a 100% sampling of your audience.
Comment author Desoja Jain Comment author email devid121<at>gmail.com Time Apr 2, 2009 01:08 GMT #2
If you circulate a publication then you want to know what your readers think and how they interact with your printed Digital publication. Unfortunately, print surveys for these publications are costly, time consuming and very rarely are they a 100% sampling of your audience.
Comment author Johnson Comment author email johnsonhicks1<at>gmail.com Time Oct 29, 2009 10:35 GMT #3
Other digital editions vendors offer software for sale or very low option self-serve models for creating digital editions.Face: Angel
Comment author marco Time Jan 6, 2010 06:22 GMT #4
Hello!
thank you for this post this helped a lot.

I wonder which commenting system you use here.. it looks so good :)
Cheers,
marco

Add comment

Message
face:happy face:glasses face:devilgrin face:crying face:grin face:laughing face:sad face:kiss face:shocked face:wink face:monkey face:angel
Post comment
Your name
Your email or website (optional)
Icon
Confirmation code
Confirmation code