在 actionscript 中,检查 xml 节点属性是否存在的最佳方法是什么? [英] In actionscript, what's the best way to check if a xml node property exists?

查看:34
本文介绍了在 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>

在针对它编写一些代码之前,在 actionscript 中检查 hasCover 属性是否存在的最佳(或接受)方法是什么?

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

推荐答案

只是为了增加一些精度.

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());

这篇关于在 actionscript 中,检查 xml 节点属性是否存在的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆