Loading a xml file using custom class

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

Vinothbabu has written 24 stories on this site.

In short, my name is Vinoth babu { perra kettala summa authrithala } a UI expert from India, Chennai.

One Comment on “Loading a xml file using custom class”

  • Micheal wrote on 3 July, 2009, 9:15

    You are awesome vinoth, it was really useful in my project

Write a Comment

Gravatars are small images that can show your personality. You can get your gravatar for free today!

Copyright © 2010 HTTPguru. All rights reserved.

Our Partners:
WebHosting Jobs| UI expert | Packet Sniffer  | Mobile Home Refinancing  | Laptop LCD screens | CSS Creator| Exchange Hosting| Compare cable companies| Usa online casino | vps | Coupon Codes