Loading a xml file using custom class
- Tuesday, April 28, 2009, 9:31
- ActionScript, Adobe Flex, Featured
- 1 comment
How nice it would be if we have a custom class which can load any xml or data file without much headache? Today while coding my application, i thought of bounding this in as it might be useful for the rest of developers to just call the file alone.
package utils {
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.system.Security;
public class UrlXloader
{private static var _instance : UrlXloader = null;
public static function get instance() : UrlXloader {
if(_instance == null){
_instance = new UrlXloader();
Security.allowDomain(‘*’);}
return _instance;
}public function loadtheFile(fileRef : String, vinothLoad : Function):void{
var request:URLRequest=new URLRequest(fileRef);
var loader:URLLoader=new URLLoader();
try
{
loader.load(request);
loader.addEventListener(Event.COMPLETE, vinothLoad);
}
catch (error:ArgumentError)
{
throw new Error(“An ArgumentError has occurred.”);
}
catch (error:SecurityError)
{
throw new Error(“A SecurityError has occurred.” + error.message);
}
}
}
}
To start with, i am possibly making sure only single instance of a class is created by using a static initializer and checking it inside the function. [ Security.allowDomain('*'); ] The wildcard (*) value permits cross-scripting operations where the accessing file is any file at all, loaded from anywhere. At the next line you return the instance.
The rest of lines below are the usual things on how to perform a request and load the request using URLLoader class.
How to use the class file in your application.
UrlXloader.instance.loadtheFile(“filename.xml”, handleVinoth);
private function handleVinoth(event:Event):void
{
var dataXML:XML = XML(event.target.data);
}
Popularity: 8% [?]
About the Author
One Comment on “Loading a xml file using custom class”
Write a Comment
Gravatars are small images that can show your personality. You can get your gravatar for free today!
You are awesome vinoth, it was really useful in my project