如何测试是否已使用AS3在XML节点中设置了属性

Bou*_*ess 9 xml actionscript-3

我正在阅读AS3中的XML文件.我需要找出节点上是否存在属性.我想做的事情如下:

if(xmlIn.attribute("id")){
foo(xmlIn.attribute("id"); // xmlIn is of type XML
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用.即使属性id不在节点上,上面的if语句也始终为true.

Jon*_*lap 18

你必须这样做:

if(xmlIn.hasOwnProperty("@id")){
    foo(xmlIn.attribute("id"); // xmlIn is of type XML
}
Run Code Online (Sandbox Code Playgroud)

在XML E4X解析中,您必须使用hasOwnProperty来检查是否在E4X XML对象节点上设置了属性的属性.希望这可以帮助!


mar*_*l82 6

我发现了4种方式:

if ('@id' in xmlIn)
if (xmlIn.hasOwnProperty("@id"))
if (xmlIn.@id.length() > 0)
if (xmlIn.attribute("id").length() > 0)
Run Code Online (Sandbox Code Playgroud)

我首选方法:

if ('@id' in xmlIn) 
{
    foo(xmlIn.@id);
}
Run Code Online (Sandbox Code Playgroud)