如果我有一些像这样的xml:
<books> <book title="this is great" hasCover="true" /> <book title="this is not so great" /> </books>
解决方法
只是为了增加一些精确度.
如果你想检查属性是否存在,即使它是空的,你肯定应该使用hasOwnProperty:
var propertyExists:Boolean = node.hasOwnProperty('@hasCover');
检查内容的长度有点脏,如果属性的值为空,则返回false.您甚至可能会抛出运行时错误,因为如果该属性不存在,您将尝试访问空对象(hasCover)上的属性(长度).
如果要测试属性是否存在且值已设置,则应尝试以hasOwnProperty开头,以便在属性不存在时忽略值test(最终运行时错误):
var propertyExistsAndContainsValue:Boolean = (node.hasOwnProperty('@hasCover') && node.@hasCover.length());