【发布时间】:2012-07-28 15:40:50
【问题描述】:
我正在使用 Flash CS6 - Adobe AIR 3.3: 我不想在我的 XML 中准确地输入我想搜索的内容,而是想使用一个可以更改的动态变量来搜索不同的类别和日期。以下是我想使用的代码:
var someCategory:String = new String("food");
var someDay:String = new String("monday");
var locationsLoader:URLLoader = new URLLoader();
locationsLoader.load(new URLRequest("http://www.myfile.xml"));
locationsLoader.addEventListener(Event.COMPLETE, init);
//load xml
function init(e:Event):void
{
theXML = new XML(e.target.data);
theXML.ignoreWhitespace = true;
e.currentTarget.close();
for(var i:int = 0; i < theXML.someCategory.length(); i++)
{
if(theXML.someCategory[i].somdDay != "un")
{
//do soemthing
}
}
此代码目前仅在我在“for”和“if”循环中实际键入“food”和“monday”时才有效。有什么建议吗?
XML 将是...
<xml>
<food>
<monday>yes</monday>
</food>
<food>
<monday>yes 2</monday>
</food>
<food>
<monday>un</monday>
</food>
</xml>
这是目前的工作方式:
var someCategory:String = new String("food");
var someDay:String = new String("monday");
var locationsLoader:URLLoader = new URLLoader();
locationsLoader.load(new URLRequest("http://www.myfile.xml"));
locationsLoader.addEventListener(Event.COMPLETE, init);
//load xml
function init(e:Event):void
{
theXML = new XML(e.target.data);
theXML.ignoreWhitespace = true;
e.currentTarget.close();
for(var i:int = 0; i < theXML.food.length(); i++)
{
if(theXML.food[i].monday != "un")
{
//do soemthing
}
}
【问题讨论】:
-
您的代码中有很多错误,请您更正一下,例如 somDay、I 和 someCategory 应该输入为字符串还是数组?您的 XML 中的日期在哪里?显示也适合您的代码。
-
很抱歉。是的,它们是字符串。我修复了 XML,并展示了当前的工作原理。
-
感谢您的更正,请参阅下面的答案,相信这可以很好地满足您的目的,忽略我将 XML 内联并摆脱了 Loader 等事实,只是为了便于检查。跨度>
标签: xml actionscript-3 air flash-cs5