我正在尝试声明将在我的flex mxml主应用程序的其他地方使用的私有变量。但是我只需要在我的xml文件中创建相同数字时才创建变量。
我的xml代码段如下所示:
<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>
我的主要flex mxml文件如下所示:
//------------------------------------------------------------------------------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;
当我的xml可能只有500个位置时,我不想声明2000个私有变量。
以下是我的xml如何添加到我的flex应用程序中:
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++;
}
我尝试了下面的类似代码,但它不起作用,因为在解析它之前没有真正定义locXML。
//------------------------------------------------------------------------------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;}
是否可以等到xml加载后再声明这些变量?
答案 0 :(得分:1)
您是否考虑过一系列地点。你的2000个变量让我头疼。
Location.as:
package my.locations.util {
public class Location {
public var latitude;
public var longitude;
public var name;
//... others, constructor
}
}
和
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);
}