在ActionScript中,什么是要检查一个XML节点属性是否存在的最佳方法是什么?

问题描述:

如果我有一些XML像这样:

If I have some xml like so:

<books>
    <book title="this is great" hasCover="true" />
    <book title="this is not so great" />
</books>

什么是在动作的最佳(或接受)的方法来检查hasCover属性写一些code对之前存在?

What's the best (or accepted) way in actionscript to check if the hasCover attribute exists before writing some code against it?

我想补充一些precisions。

Just to add some precisions.

如果您要检查该属性存在,即使它是空的,你一定要使用的hasOwnProperty:

If you want to check if the property exists even though it's empty you should definitely use hasOwnProperty :

var propertyExists:Boolean = node.hasOwnProperty('@hasCover');

检查内容的长度是以某种方式脏,将返回false,如果该属性的值为空。你甚至可以有一个运行时错误抛出,你会试图访问一个属性(长度)的情况下一个空对象(hasCover)的属性不存在。

Checking the length of the content is somehow dirty and will return false if the value of the attribute is empty. You may even have a run-time error thrown as you will try to access a property(length) on a null object (hasCover) in case the attribute doesn't exist.

如果你想测试,如果该属性存在并且该值设置你应该尝试两种首先是的hasOwnProperty ,以便值测试(最终运行时错误)的情况下,该属性被忽略不存在:

If you want to test if the property exists and the value is set you should try both starting with the hasOwnProperty so that the value test (eventual run-time error) gets ignored in case the attribute doesn't exist :

var propertyExistsAndContainsValue:Boolean = (node.hasOwnProperty('@hasCover') && node.@hasCover.length());