2008-09-29 91 views
9

我使用e4x結果格式從HTTPService調用中獲取XML響應。確定Flex中是否存在XML屬性的最佳方法

 

<?xml version="1.0" encoding="utf-8"?> 
<Validation Error="Invalid Username/Password Combination" /> 
 

我曾嘗試:

 

private function callback(event:ResultEvent):void { 
    if([email protected]) { 
     // error attr present 
    } 
    else { 
     // error attr not present 
    } 
} 
 

這似乎並不工作(它總是認爲錯誤屬性退出)是什麼做到這一點的最好方法是什麼?謝謝。

編輯:我也試圖比較空的屬性,並沒有這樣的成功,一個空字符串...

+0

檢查我的答案在最後。我相信這就是你一直在尋找的! :) – Rihards 2010-10-25 22:08:48

回答

1

我已經找到了一個解決方案,我仍然有興趣,如果有更好的辦法要做到這一點...

這將工作:

 

private function callback(event:ResultEvent):void { 
    if(event.result.attribute("Error").length()) { 
     // error attr present 
    } 
    else { 
     // error attr not present 
    } 
} 
 
2

假設在你的例子event.resultXML對象,它的內容是完全按照你貼出來,這應該工作(由於這樣的事實,驗證標記是XML的根標籤):

var error:String = [email protected]; 
if (error != "") 
    // error 
else 
    // no error 

上面的示例將假定具有空值的現有Error屬性應作爲一個「NO-待處理錯誤」的情況下,雖然如此,如果你想知道如果屬性實際上存在與否,你應該這樣做:

if (event.result.hasOwnProperty("@Error")) 
    // error 
else 
    // no error 
11

您發現這樣做的最佳方式:

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

如果您不知道它們是否存在,attribute方法是檢索屬性的首選方法。

+0

我認爲重要的是強調這不等於`event.result.Error.length()> 0`,如果Error不存在將會失敗。欲瞭解更多信息,請參閱:http://www.adobe.com/livedocs/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000125.html – sixtyfootersdude 2011-02-18 22:21:20

+0

我想你的意思是,「事件.result。@ Error「,因爲event.result.Error會查找名爲」Error「的節點而不是該屬性。 – 2013-06-13 11:20:30

3

您可以通過以下方式檢查:

if (undefined == [email protected]) 

或動態

if (undefined == [email protected][attributeName]) 

注意,在你的榜樣,這兩個點會檢索所有級別的所有後代,所以你會得到結果列表。如果沒有錯誤屬性,你會得到一個空的列表。這就是爲什麼它永遠不會等於空。

5

我喜歡這種方法,因爲a。)它很痛苦簡單,而且b。)Ely Greenfield使用它。 ;)

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

我喜歡用下面的語法檢查,因爲它很容易閱讀,少打字和它在伯仲之間的最快方法:

if ("@style" in item) // do something 

要當你不分配一個值返回給該屬性「知道它的名字前手使用attribute方法:

var attributeName:String = "style"; 
var attributeWithAtSign:String = "@" + attributeName; 
var item:XML = <item style="value"/>; 
var itemNoAttribute:XML = <item />; 

if (attributeWithAtSign in itemNoAttribute) { 
    trace("should not be here if attribute is not on the xml"); 
} 
else { 
    trace(attributeName + " not found in " + itemNoAttribute); 
} 

if (attributeWithAtSign in item) { 
    item.attribute(attributeName)[0] = "a new value"; 
} 

以下所有的方法來測試,如果一個屬性存在,從這個疑問句列出的答案雲集灰。由於我在11.7.0.225調試播放器中運行了很多次,右邊的值是使用的方法。左邊的值是運行代碼一百萬次所用的最低時間(以毫秒爲單位)。下面是結果:

807 item.hasOwnProperty("@style") 
824 "@style" in item 
1756 [email protected][0] 
2166 (undefined != [email protected]["style"]) 
2431 (undefined != item["@style"]) 
3050 XML(item).attribute("style").length()>0 

性能測試代碼:

var item:XML = <item value="value"/>; 
var attExists:Boolean; 
var million:int = 1000000; 
var time:int = getTimer(); 

for (var j:int;j<million;j++) { 
    attExists = XML(item).attribute("style").length()>0; 
    attExists = XML(item).attribute("value").length()>0; 
} 

var test1:int = getTimer() - time; // 3242 3050 3759 3075 

time = getTimer(); 

for (var j:int=0;j<million;j++) { 
    attExists = "@style" in item; 
    attExists = "@value" in item; 
} 

var test2:int = getTimer() - time; // 1089 852 991 824 

time = getTimer(); 

for (var j:int=0;j<million;j++) { 
    attExists = (undefined != [email protected]["style"]); 
    attExists = (undefined != [email protected]["value"]); 
} 

var test3:int = getTimer() - time; // 2371 2413 2790 2166 

time = getTimer(); 

for (var j:int=0;j<million;j++) { 
    attExists = (undefined != item["@style"]); 
    attExists = (undefined != item["@value"]); 
} 

var test3_1:int = getTimer() - time; // 2662 3287 2941 2431 

time = getTimer(); 

for (var j:int=0;j<million;j++) { 
    attExists = item.hasOwnProperty("@style"); 
    attExists = item.hasOwnProperty("@value"); 
} 

var test4:int = getTimer() - time; // 900 946 960 807 

time = getTimer(); 

for (var j:int=0;j<million;j++) { 
    attExists = [email protected][0]; 
    attExists = [email protected][0]; 
} 

var test5:int = getTimer() - time; // 1838 1756 1756 1775 
相關問題