开发者

create private var yada:Whatever; depending on xml count

I am trying to declare private variables that will be used elsewhere in my flex mxml main application. But I need to only create the variables when their is an equal number in my xml file.

My xml snippet looks like this:

<POIs>
    <location>
            <name>jane</name>
            <lat>12345</lat>
            <long>12345</long>
            <iconFile>jane_icon.gif</iconFile>
            <imageFile>jane_img.swf</imageFile>
            <motion>no</motion>
    </location>
    <location>
            <name>john</name>
            <lat>23456</lat>
            <long>23456</long>
            <iconFile>john_icon.gif</iconFile>
            <imageFile>john_img.swf</imageFile>
            <motion>no</motion>开发者_如何学编程;
    </location>
</POIs>

My main flex mxml file looks like this:

//------------------------------------------------------------------------------0
private var locPointMarker0:PointMarker;
private var locPointMarker0_lat:Number;
private var locPointMarker0_long:Number;

//------------------------------------------------------------------------------1
private var locPointMarker1:PointMarker;
private var locPointMarker1_lat:Number;
private var locPointMarker1_long:Number;

//------------------------------------------------------------------------------2
private var locPointMarker2:PointMarker;
private var locPointMarker2_lat:Number;
private var locPointMarker2_long:Number;

//------------------------------------------------------------------------------3
private var locPointMarker3:PointMarker;
private var locPointMarker3_lat:Number;
private var locPointMarker3_long:Number;

I don't want to declare 2000 private variables when my xml might only have 500 locations.

Here is how my xml is being added to my flex app:

public function getXMLData():void
{   
    var locXMLLoader:URLLoader = new URLLoader(locXMLURL);
    locXMLLoader..addEventListener(Event.COMPLETE, xmlLoaded);  
    function xmlLoaded(e:Event):void
    {
        try
        {
            locXML = XML(locXMLLoader.data);
            locXML.location.(parseLocXML(
                valueOf().name.toString(),
                parseFloat(valueOf().lat.toString()),
                parseFloat(valueOf().long.toString()),
                       valueOf().iconFile.toString(),
                       valueOf().imageFile.toString(),
                       valueOf().motion.toString()));
        } catch(e:Error)
        {
            trace("XML Load Error:  " + e.message)
            return;
        }
    }
}

public function parseLocXML(nodeName:String, lat:Number, 
            long:Number, iconFile:String, 
            imageFile:String, motion:String):void

{   
    if (XMLpCounter==0)
    {
    addMarkers(); ///this is just one of the functions that requires the private vars
    }
    XMLpCounter++;
}

I tried the similar code below, but it doesn't work because locXML isn't defined really till its is parsed.

//------------------------------------------------------------------------------1
if(locXML.location.length()>=1){
    trace("1 has been added as a variable");
    private var locPointMarker1:PointMarker;
    private var locPointMarker1_lat:Number;
    private var locPointMarker1_long:Number;}
            //------------------------------------------------------------------------------2
if(locXML.location.length()>=2){
    trace("2 has been added as a variable");
    private var locPointMarker2:PointMarker;
    private var locPointMarker2_lat:Number;
    private var locPointMarker2_long:Number;}

Is it possible to wait until the xml has loaded before declaring these variables?


Have you thought about an array of locations. Your 2000 variables make my head hurt.

Location.as:

package my.locations.util    {
    public class Location {
        public var latitude;
        public var longitude;
        public var name;
        //... others, constructor
    }
 }

and

 var locations:Array = [];
 var locationsXML:XML = //...
 for (var locationXML:XML in locationsXML.location) {
     var location:Location = new Location(locationXML.latitude, locationXML.longitude, ...);
     locations.push(location);
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜