簡體   English   中英

在 actionscript 中,檢查 xml 節點屬性是否存在的最佳方法是什么?

[英]In actionscript, what's the best way to check if a xml node property exists?

如果我有一些 xml 像這樣:

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

actionscript 在編寫一些代碼之前檢查 hasCover 屬性是否存在的最佳(或接受)方法是什么?

只是為了增加一些精度。

如果你想檢查屬性是否存在,即使它是空的,你絕對應該使用 hasOwnProperty:

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

檢查內容的長度有點臟,如果屬性的值為空,將返回 false。 您甚至可能會拋出運行時錯誤,因為如果該屬性不存在,您將嘗試訪問 null object (hasCover) 上的屬性(長度)。

如果您想測試屬性是否存在並且值是否已設置,您應該嘗試從 hasOwnProperty 開始,以便在屬性不存在的情況下忽略值測試(最終的運行時錯誤):

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

好的 - 我今天遇到了這個問題,a.)它被 Ely Greenfield 使用過,b.)它非常簡單,所以我必須將其標記為答案,除非有人能找到不這樣做的理由......

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

請參閱問題 #149206:“確定 Flex 中是否存在 XML 屬性的最佳方法”

我建議做event.result.hasOwnProperty("@attrName")但是 Theo 最贊成的答案(在撰寫本文時)表明這一點:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM