Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在actionscript中,什么';检查xml节点属性是否存在的最佳方法是什么?_Xml_Apache Flex_Flash_Actionscript 3_Properties - Fatal编程技术网

在actionscript中,什么';检查xml节点属性是否存在的最佳方法是什么?

在actionscript中,什么';检查xml节点属性是否存在的最佳方法是什么?,xml,apache-flex,flash,actionscript-3,properties,Xml,Apache Flex,Flash,Actionscript 3,Properties,如果我有这样一些xml: <books> <book title="this is great" hasCover="true" /> <book title="this is not so great" /> </books> 在actionscript中,在编写代码之前,检查hasCover属性是否存在的最佳(或公认)方法是什么?请参阅 我建议执行event.result.hasOwnProperty(@attrName”

如果我有这样一些xml:

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

在actionscript中,在编写代码之前,检查hasCover属性是否存在的最佳(或公认)方法是什么?

请参阅

我建议执行
event.result.hasOwnProperty(@attrName”)
,但(在撰写本文时)Theo建议:

event.result.attribute("attrName").length() > 0

只是为了增加一些精确性

如果要检查属性是否存在,即使该属性为空,也必须使用hasOwnProperty:

var propertyExists:Boolean = node.hasOwnProperty('@hasCover');
检查内容的长度有点脏,如果属性值为空,则返回false。您甚至可能会抛出运行时错误,因为如果属性不存在,您将尝试访问null对象(hasCover)上的属性(length)

如果要测试属性是否存在且值是否已设置,则应尝试从hasOwnProperty开始的两种,以便在属性不存在时忽略值测试(最终运行时错误):

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

好的-我今天遇到了这个问题,a.)这是伊利·格林菲尔德和b.)使用的,非常简单,所以我必须将它标记为答案,除非有人能找到不

if("@property" in node){//do something}

肯定是要走的路。我们的整个平台(Mockingbird)构建在XML文档之上,这就是我检查属性(或子元素)存在的方式。我相信它的字节码相当于hasOwnProperty,但看起来更可读(在我看来)。node@hasCover.length()应该检查node返回的XMLList的长度。@hasCover,而不是hasCover属性的字符串值的长度。我说“应该”,因为这里我们碰到了E4X内置的讨厌的自动类型转换。要检查属性值的长度,我将使用node@hasCover[0].length。这确保我们获得返回的XMLList中的第一个值,然后使用String.length属性检查内容的长度。