1

我正在使用 Flash CS6 - Adob​​e 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
}
}
4

2 回答 2

0

I would use a conditional statement and use a for each loop instead of a for loop:

var cat:String = new String("food");
var day:String = new String("monday");

for each (var node:XML in theXML[cat].(child(day) != 'un')) {
    trace(node[day]);
}

The theXML[cat].(child(day) != 'un') part means

(loop) for each children of theXML with node name [cat] having child node with node name [day] the value of which is not 'un'.

于 2012-07-30T23:39:42.610 回答
0

复制您的代码并逐步更改每个部分,并且我所做的每次更改似乎都得到了相同的结果,所以我相信下面的内容是正确的。

package
{
    import flash.display.Sprite;

    public class TestAS3Project extends Sprite
    {
        public var theXML:XML = new XML(<xml>
                <food>
                    <monday>yes</monday>
                </food>
                <food>
                    <monday>yes 2</monday>
                </food>
                <food>
                    <monday>un</monday>
                </food>

            </xml>);
        public function TestAS3Project()
        {
            init();
        }
        private function init():void
        {

            var someCategory:String = new String("food");
            var someDay:String = new String("monday");

            theXML.ignoreWhitespace = true;

            for(var i:int = 0; i < theXML[someCategory].length(); i++) 
            {
                if(theXML[someCategory][i][someDay] != "un")
                {
                    //do soemthing
                    trace(theXML[someCategory][i][someDay]);
                }
            }
        }
    }
}
于 2012-07-30T23:04:17.087 回答