Loading xml from another domain

I have a flash file that uses actionscript to read a remote XML file. Thanks to kirupa.com for the tutorial that allowed me to accomplish this. Everything is working pefectly except one little issue.

When the movie loops it requests the XML file again. How can I have it request the XML file only the first time and just re-utilize the data?

Thanks in advance.

many ways to fix it. the easiest is probably to use a boolean and check it with a conditional before you load the xml. something like this:

if(!xmlLoaded){
xmlLoaded=true;
myXML.load(“myxml.xml”);
}

that works perfectly! thank you very much. another side problem though. down in the status bar of the browser it always says “transferring data from <hostname>”

do i need to terminate the connection or something so that it will stop doing that?

are you using

.onLoad

to create a callback function when the xml loads?

In other words are you sure that the xml has loaded before you’re attempting to do something with it?

The xml load should occur and then stop. There’s no connection that should continue.

Do you have a ginormous amount of xml or something and it’s taking awhile to load?

my code is below, hopefully that helps. =) and no, the xml file is super small - like under 50 characters total.

function movieName () 
{
    var url = new String (this._url);
    var p1 = url.lastIndexOf  ("/") + 1;
    var p2 = url.length;
    return url.slice (p1, p2);
}
function loadXML(loaded)
{
    if (loaded)
    {
    _root.c_rank = this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue;
    _root.h_rank = this.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue;
    }         else {
              return false;
            }
}
// SET VARIABLES
name = movieName();
var url = 'http://www.example.com/file.xml';

xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;

if(!xmlLoaded){
xmlLoaded=true;
xmlData.load(url);
}

try this:

function movieName () 
{
    var url = new String (this._url);
    var p1 = url.lastIndexOf  ("/") + 1;
    var p2 = url.length;
    return url.slice (p1, p2);
}
// SET VARIABLES
name = movieName();
var url = 'http://www.example.com/file.xml';

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

//callback function for xml load usually goes here right after constructor
xmlData.onLoad=function(success){
if(success)
{    _root.c_rank = this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue;
    _root.h_rank = this.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue;
    }         else {
              return false;
            }
}
}



if(!xmlLoaded){
xmlLoaded=true;
xmlData.load(url);
}

your problem may be that you’re declaring a callback ‘.onLoad’ function before you create the object it’s associated with. Also you had

xmlData.onLoad = loadXML;

which should probably be:

xmlData.onLoad = loadXML();

(you forgot the parenthesis in there. You don’t need a seperate function like that though. I put everything into the .onLoad handler for you.

i copied and paste what you have above and it works just fine - but i still have the same problem with the flash file “looking” like it is continuously requesting data on the browser status bar. i am not calling an xml file directly, i am actually calling a php file that outputs in xml format - would that cause a problem?

you have:

var url = 'http://www.example.com/file.xml'

where are you loading the php file?

post the code that does that and your php code and I’ll take a look (i’m no PHP expert but I can usually figger out what I need to do)

And No, that shouldn’t cause a problem in and of itself. That’s pretty common practice.

all the rest of the code is exactly the same except the line where the url var gets set - which is below.

var url = 'http://www.example.com/xml.php?item=' + name;

i am pretty solid with the PHP and don’t believe there are any problems there - are there any specific guidelines to using PHP with actionscript that i should know about?

notice anything out of the ordinary with that? if not, i will put up some test files so that i can give you complete access to everything. =)