• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 348
  • Last Modified:

How do I load random xml-file

I want my flashmovie to act in different ways, depending on the name of a xml-file.
Which means that if a xml-file does not exist, nothing will happen, but if xmlfile5.xml exist the swf will behave in another way, than if the file existing is named xmlfile7.xml.
But how do I tell in as3 the swf to accept all filenames in the range xmlfile1 to xmlfile10.
Off course only one at a time - and always the lowest - or highest - number.
Something like the attached snippet??
var xmlname="xmlfile"+xmlnumbers;
myXML = new XML();
XML_URL = xmlname+".xml";
myXMLURL = new URLRequest(XML_URL);
myxmlLoader = new URLLoader(myXMLURL);
myxmlLoader.addEventListener("complete", myxmlLoaded);
myxmlLoader.addEventListener("ioError", xmlLoadError);

Open in new window

0
petersego
Asked:
petersego
  • 6
  • 5
  • 2
1 Solution
 
deepanjandasCommented:
var xmlnumbers:Number = Math.round( Math.random()*9 ) + 1;

Warm Regards
Deepanjan Das
0
 
petersegoAuthor Commented:
Sorry, maybe my headline was misleading.
I do not want to load a random xml-file, but a specific xmlfile - if it exist.
So that in the loading-proces, the path is searched for the first file wth the name xmlfile and a number behind it.
If there is one - or several - , it - or the one with the lowest number - is loaded, if not, the loadError-event is executed.

I hope Ive made the question more clear now.
0
 
deepanjandasCommented:
Yes, that is what I have done.

var xmlnumbers:Number = Math.round( Math.random()*9 ) + 1;
var xmlname="xmlfile"+xmlnumbers;
myXML = new XML();
XML_URL = xmlname+".xml";
myXMLURL = new URLRequest(XML_URL);
myxmlLoader = new URLLoader(myXMLURL);
myxmlLoader.addEventListener("complete", myxmlLoaded);
myxmlLoader.addEventListener("ioError", xmlLoadError);

the XML_URL will be something like this - xmlfile1.xml, xmlfile2.xml....till xmlfile10.xml randomly

Warm Regards
Deepanjan Das
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
petersegoAuthor Commented:
Im sorry, i still cant have explained myself correctly.
I have a xml-file named xmlfile5, that i want to be loaded. All that the swf-movie know is that it should load a xml-file, that starts with xmlfile followed by a number. Now, because there is a file named xmlfile5, the swf should load xmlfile5. If there was also a file named xmlfile4, it should load xmlfile4.
But even though I have a file called xmlfile5.xml, it is not loaded. But it should be loaded, since it has a number between 1 and 10.
But the trace shows that the loadErrorEvent is executed.
So i was thinking more in the line of an array of numbers.
Im sorry if I cant explain the question well enough.
0
 
deepanjandasCommented:
Its my bad that I was not able to understand.
Anyways...
Did you check the path.
Where are the xmls located related to your swf and html file?

Warm Regards
Deepanjan Das
0
 
petersegoAuthor Commented:
Oh, there is nothing wrong with the path.
When I physically writes xmlname as xmlfile5, it does load xmlfile5. But that is the point. The swf-movie will not know that it should load xmlfile5, it only know that it should load files that starts with xmlfile followed by a number. And since I have decided to upload xmlfile5 to the same folder, it should load xmlfile5. If I later on upload xmlfile4 to the folder, the swf-movie should load xmlfile4, not xmlfile5.
0
 
deepanjandasCommented:
okie so do this:

var xmlnumbers:Number = Math.round( Math.random()*9 ) + 1;
var xmlname="xmlfile"+String(xmlnumbers);
myXML = new XML();
XML_URL = xmlname+".xml";

Warm Regards
Deepanjan Das
0
 
petersegoAuthor Commented:
That will still result in that only one out of ten times it will load xmlfile5. It should load xmlfile5 everytime if xmlfile5 is there.
0
 
deepanjandasCommented:
Did you typecase the number variable like this String(xmlnumbers)?

Warm Regards
Deepanjan Das
0
 
blue-genieCommented:
petersego. are you still requiring assistance on this?
can you follow up with Deepanjandas or provide a snippet of the code you're done?

if there's a file called xml4.xml how is flashed supposed to know that?
0
 
petersegoAuthor Commented:
Sorry, but I felt we were going in circles and started on another approach, that I should have told you about. Sorry again.

I made the code below, that checks the xmlfilename for the last number and try if it can load the file. If it cannot load the file it goes through function adsLoadError and checks again for a number -1 untill it finds a xmlfile with a number it can load.
var myAdsURL:URLRequest;
var myAdsLoader:URLLoader;
var adsXML:XML;
var ads_URL;
var xmlname="xmlfile";
var gnum:Number;
var gname:String;
function getXMLads(adsname){
	adsXML = new XML();
	ads_URL = "http://mysite.com/"+adsname+".xml";
	myAdsURL = new URLRequest(ads_URL);
	myAdsLoader = new URLLoader(myAdsURL);
	myAdsLoader.addEventListener("complete", adsLoaded);
	myAdsLoader.addEventListener("ioError", adsLoadError);
}
function checkxmlnamenum(tnum){
	gnum=tnum;
	gname=xmlname+gnum;
	getXMLads(gname);
}
checkxmlnamenum(10);
function adsLoadError(e:IOErrorEvent):void{
	gnum=gnum-1;
	if (gnum>=1){
		gname=xmlname+gnum;
		getXMLads(gname);
	}
	else {
		gname="activation"
		checkActivation();
	}
}
function checkActivation(){
	trace("Activation");
}
function adsLoaded(event:Event):void{
	trace("Loaded");
}

Open in new window

0
 
blue-genieCommented:
ok if you're happy with your new approach then close your question please.

why don't you use something like php to go and check for the existence of the file in the folder?
php can return all xml files in the location, pass it to the flash and the flash can load it.
that would be less processor intensive i'm sure.
0
 
petersegoAuthor Commented:
I managed to figure out this myself
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now