确定Flex中是否存在XML属性的最佳方法

mma*_*tax 9 javascript apache-flex actionscript-3

我使用e4x结果格式从HTTPService调用获得XML响应.


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

我试过了:


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

这似乎不起作用(它总是认为错误属性退出)这样做的最佳方法是什么?谢谢.

编辑:我也尝试将属性与null和空字符串进行比较,但没有成功...

The*_*heo 11

您找到了最好的方法:

event.result.attribute("Error").length() > 0
Run Code Online (Sandbox Code Playgroud)

attribute如果您不知道它们是否存在,则该方法是检索属性的首选方法.


one*_*ney 5

我喜欢这种方法,因为a.它很简单而且很简单.)Ely Greenfield使用它.;)

if("@property" in node){//do something}
Run Code Online (Sandbox Code Playgroud)


mma*_*tax 1

我已经找到了解决方案,我仍然感兴趣是否有更好的方法来做到这一点......

这将起作用:


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