Hello
I am working on a project that loads XML file, parses it and then creates the interface. I just wanna ask whether the way I am doing this is efficient if not what other best ways could be possible.
The code is give as under.
//
package
{
import flash.events.*;
import flash.net.*;
import flash.xml.*;
/* LoadXMl class loads an XML file and assigns the XMLList children
to its property xmlList. It also dispatches an event when the loading
is complete
*/
public class XmlLoader extends EventDispatcher
{
// Class properties
private var _xml:XML;
private var _xmlFileName:String;
// Class Constructor
public function XmlLoader(fileName:String)
{
_xmlFileName=fileName;
var xmlLoader:URLLoader=new URLLoader ;
xmlLoader.addEventListener(Event.COMPLETE,onComplete);
xmlLoader.load(new URLRequest(_xmlFileName));
}
public function onComplete(e:Event):void
{
_xml=XML(e.target.data);
// dispatch complete event to notify the listeners
dispatchEvent(new Event(Event.COMPLETE));
}
public function get xml():XML
{
return _xml;
}
}
}
The second class XMLParser recieves an XMLList element and then iterate through each node and makes object of class SiteContent
package com.abinteractive
{
//import flash.display.*;
import flash.xml.*;
import SiteContent;
public class XMLParser
{
// Class Constructor
public function ProcessXML()
{
}// end constructor
public static function parseXml(xml:XML)
{
for (var i:int=0; i < xml.*.length(); i++)
{
var nam:String=xml.**.@name;
var desc:String=xml.**.desc;
// creates objects of type SiteContent by passing name and desc to const
var _siteContent:SiteContent=new SiteContent(nam, desc);
}// end of for loop
} // end static
} // end class
}// end package
The third class SiteContent set the name and description properties for each object and adds them to an array which can be retrieved by other classes the lenght of this array is the number of objects or links that will be created in the interface.
// This is a template to create flash classes
package com.abinteractive
{
import flash.display.*;
public class SiteContent
{
// Class properties
private var _name:String;
private var _desc:String;
private static var _children:Array = new Array();
// Class Constructor
public function SiteContent(name:String, desc:String)
{
this._name = name;
this._desc = desc;
SiteContent._children.push(this);
}
public static function getChildren():Array
{
return SiteContent._children;
}
public function get name():String
{
return _name;
}
public function get desc():String
{
return _desc;
}
}
}
So now If I want to create buttons I just call SiteContent.getChildren(); and then iterate throught the array to make each button. Please suggest any other way that can be better or more efficient.
Thanks in Advance