检查XML属性的值

时间:2011-05-20 03:26:25

标签: xml flash actionscript-3

不确定如何检查节点属性“action”的值是否等于“left”。我的XML如下所示:

    <track timestamp="18/05/2011 13:21:49-0400" id="3" action="track">
               <coordinates> test 1</coordinates>
    </track>
    <track timestamp="18/05/2011 13:21:49-0400" id="3" action="track">
               <coordinates> test 1</coordinates>
    </track>
    <track timestamp="18/05/2011 13:21:49-0400" id="3" action="left">
                <coordinates> test 1</coordinates>
    </track>
    <track timestamp="18/05/2011 13:21:49-0400" id="3" action="track">
        <coordinates> test 1</coordinates>
    </track>

我想我需要遍历xml数据并检查它是否真正启动我的功能。这就是我到目前为止所做的:

var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("get_data.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);

               function processXML(e:Event):void {

            myXML = new XML(e.target.data);

                            for (var i:int = 0; i<myXML.*.length(); i++){
                                 if (myXML.tracking[i].action == "left")
                                    {
                                         start_animation();
                                    }
                            };
        }

1 个答案:

答案 0 :(得分:2)

您可以选择具有特定值的所有节点,如下所示:

node.(@attribute == "value")

例如,这只会输出side设置为"left"的节点:

var xml:XML = 
<list>
    <node side="left">hi</node>
    <node side="left">there</node>
    <node side="right">marty</node>
</list>;

trace(xml.node.(@side == "left"));

输出:

<node side="left">hi</node>
<node side="left">there</node>

您可以将其分配给XMLList,如下所示:

var list:XMLList = xml.node.(@side == "left");

然后像往常一样迭代:

var i:XML;
for each(i in list)
{
    trace(i);
}

回应您的评论问题:

您可以使用length()属性来生成将其边设置为左侧的节点数,如下所示:

var xml:XML = 
<list>
    <node side="left">hi</node>
    <node side="left">there</node>
    <node side="right">marty</node>
</list>;

var list:XMLList = xml.node.(@side == "left");

// check if a node was found
if(list.length() > 0)
{
    doStuff();
}

function doStuff():void
{
    trace("a node had its side set as left");
}