# Loading xml from another domain

**URL:** <https://forum.kirupa.com/t/loading-xml-from-another-domain/184162>\
**Category:** Uncategorized\
**Created:** [April 2, 2006, 9:04pm UTC](https://forum.kirupa.com/t/loading-xml-from-another-domain/184162 "2006-04-02T21:04:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevek1977](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@stevek1977](https://forum.kirupa.com/u/stevek1977)\
**Post date:** [April 2, 2006, 9:04pm UTC](https://forum.kirupa.com/t/loading-xml-from-another-domain/184162/1 "2006-04-02T21:04:01Z")

</div>

I have a flash file that uses actionscript to read a remote XML file. Thanks to [kirupa.com](http://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.

---

<div class="post-metadata">

**Author:** ![natronp](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@natronp](https://forum.kirupa.com/u/natronp)\
**Post date:** [April 3, 2006, 3:11am UTC](https://forum.kirupa.com/t/loading-xml-from-another-domain/184162/2 "2006-04-03T03:11:55Z")

</div>

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”);  
}

---

<div class="post-metadata">

**Author:** ![stevek1977](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@stevek1977](https://forum.kirupa.com/u/stevek1977)\
**Post date:** [April 3, 2006, 5:03am UTC](https://forum.kirupa.com/t/loading-xml-from-another-domain/184162/3 "2006-04-03T05:03:59Z")

</div>

> [@natronp](#):
>
> 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?

---

<div class="post-metadata">

**Author:** ![natronp](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@natronp](https://forum.kirupa.com/u/natronp)\
**Post date:** [April 3, 2006, 12:22pm UTC](https://forum.kirupa.com/t/loading-xml-from-another-domain/184162/4 "2006-04-03T12:22:40Z")

</div>

are you using

```auto
.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?

---

<div class="post-metadata">

**Author:** ![stevek1977](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@stevek1977](https://forum.kirupa.com/u/stevek1977)\
**Post date:** [April 3, 2006, 12:31pm UTC](https://forum.kirupa.com/t/loading-xml-from-another-domain/184162/5 "2006-04-03T12:31:47Z")

</div>

> [@natronp](#):
>
> are you using
> 
> ```auto
> .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.

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![natronp](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@natronp](https://forum.kirupa.com/u/natronp)\
**Post date:** [April 3, 2006, 12:38pm UTC](https://forum.kirupa.com/t/loading-xml-from-another-domain/184162/6 "2006-04-03T12:38:59Z")

</div>

try this:

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![stevek1977](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@stevek1977](https://forum.kirupa.com/u/stevek1977)\
**Post date:** [April 3, 2006, 1:37pm UTC](https://forum.kirupa.com/t/loading-xml-from-another-domain/184162/7 "2006-04-03T13:37:01Z")

</div>

> [@natronp](#):
>
> try this:
> 
> ```auto
> 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 which should probably be:
> 
> ```auto
> 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?

---

<div class="post-metadata">

**Author:** ![natronp](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@natronp](https://forum.kirupa.com/u/natronp)\
**Post date:** [April 3, 2006, 1:40pm UTC](https://forum.kirupa.com/t/loading-xml-from-another-domain/184162/8 "2006-04-03T13:40:20Z")

</div>

you have:

```auto
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.

---

<div class="post-metadata">

**Author:** ![stevek1977](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@stevek1977](https://forum.kirupa.com/u/stevek1977)\
**Post date:** [April 3, 2006, 6:09pm UTC](https://forum.kirupa.com/t/loading-xml-from-another-domain/184162/9 "2006-04-03T18:09:32Z")

</div>

> [@natronp](#):
>
> you have:
> 
> ```auto
> 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.

```auto
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?

---

<div class="post-metadata">

**Author:** ![stevek1977](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@stevek1977](https://forum.kirupa.com/u/stevek1977)\
**Post date:** [April 4, 2006, 10:48pm UTC](https://forum.kirupa.com/t/loading-xml-from-another-domain/184162/10 "2006-04-04T22:48:38Z")

</div>

> [@stevek1977](#):
>
> all the rest of the code is exactly the same except the line where the url var gets set - which is below.
> 
> ```auto
> 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. =)
