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

时间:2022-09-13 20:31:32

I have a XML response from an HTTPService call with the e4x result format.

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


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

I have tried:

我试过了:


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

This does not seem to work (it always thinks that the error attribute exits) what is the best way to do this? thanks.

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

EDIT: I have also tried to compare the attribute to null and an empty string without such success...

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

7 个解决方案

#1


11  

You have found the best way to do it:

您找到了最好的方法:

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

The attribute method is the preferred way to retrieve attributes if you don't know if they are there or not.

如果您不知道属性是否存在,则属性方法是检索属性的首选方法。

#2


5  

I like this method because a.) it's painfully simple and b.) Ely Greenfield uses it. ;)

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

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

#3


3  

You can check this in the following way:

您可以通过以下方式检查:

if (undefined == event.result.@Error)

or dynamically

if (undefined == event.result.@[attributeName])

Note that in your example, the two dots will retrieve all descendants on all levels so you'll get a list as a result. If there are no Error attributes, you'll get an empty list. That's why it will never equal null.

请注意,在您的示例中,两个点将检索所有级别上的所有后代,因此您将获得一个列表作为结果。如果没有Error属性,您将获得一个空列表。这就是它永远不会等于null的原因。

#4


2  

Assuming that in your example event.result is an XML object the contents of which are exactly as you posted, this should work (due to the fact that the Validation tag is the root tag of the XML):

假设在您的示例中,event.result是一个XML对象,其内容与您发布的完全相同,这应该可行(因为Validation标记是XML的根标记):

var error:String = event.result.@Error;
if (error != "")
    // error
else
    // no error

The above example will assume that an existing Error attribute with an empty value should be treated as a "no-error" case, though, so if you want to know if the attribute actually exists or not, you should do this:

上面的例子假设一个具有空值的现有Error属性应该被视为“无错误”的情况,所以如果你想知道该属性是否真的存在,你应该这样做:

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

#5


2  

I like to use the following syntax to check because it's easy to read, less typing and it nearly tied as the fastest method:

我喜欢使用以下语法来检查,因为它易于阅读,更少键入,并且它几乎是最快的方法:

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

To assign a value back to that attribute when you don't know the name of it before hand use the attribute method:

要在手动使用属性方法之前不知道其名称时将值赋值回该属性:

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";
}

All of the following are ways to test if an attribute exists gathered from the answers listed on this question. Since there were so many I ran each in the 11.7.0.225 debug player. The value on the right is the method used. The value on the left is the lowest time in milliseconds it takes when running the code one million times. Here are the results:

以下所有方法都是测试是否存在从此问题中列出的答案中收集的属性的方法。因为我在11.7.0.225调试播放器中运行了很多。右边的值是使用的方法。左边的值是运行代码一百万次时所用的最短时间(以毫秒为单位)。结果如下:

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

Performance Test code:

性能测试代码:

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 != item.@["style"]);
    attExists = (undefined != item.@["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 = item.@style[0];
    attExists = item.@value[0];
}

var test5:int = getTimer() - time; // 1838 1756 1756 1775

#6


1  

I have figured out a solution, I'm still interested if there is a better way to do this...

我找到了一个解决方案,如果有更好的方法,我仍然感兴趣......

This will work:

这将有效:


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

#7


0  

Here you go:

干得好:

if(event.result.@error[0]){
    //exists 
}

Easy, eh? :)

容易,嗯? :)

#1


11  

You have found the best way to do it:

您找到了最好的方法:

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

The attribute method is the preferred way to retrieve attributes if you don't know if they are there or not.

如果您不知道属性是否存在,则属性方法是检索属性的首选方法。

#2


5  

I like this method because a.) it's painfully simple and b.) Ely Greenfield uses it. ;)

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

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

#3


3  

You can check this in the following way:

您可以通过以下方式检查:

if (undefined == event.result.@Error)

or dynamically

if (undefined == event.result.@[attributeName])

Note that in your example, the two dots will retrieve all descendants on all levels so you'll get a list as a result. If there are no Error attributes, you'll get an empty list. That's why it will never equal null.

请注意,在您的示例中,两个点将检索所有级别上的所有后代,因此您将获得一个列表作为结果。如果没有Error属性,您将获得一个空列表。这就是它永远不会等于null的原因。

#4


2  

Assuming that in your example event.result is an XML object the contents of which are exactly as you posted, this should work (due to the fact that the Validation tag is the root tag of the XML):

假设在您的示例中,event.result是一个XML对象,其内容与您发布的完全相同,这应该可行(因为Validation标记是XML的根标记):

var error:String = event.result.@Error;
if (error != "")
    // error
else
    // no error

The above example will assume that an existing Error attribute with an empty value should be treated as a "no-error" case, though, so if you want to know if the attribute actually exists or not, you should do this:

上面的例子假设一个具有空值的现有Error属性应该被视为“无错误”的情况,所以如果你想知道该属性是否真的存在,你应该这样做:

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

#5


2  

I like to use the following syntax to check because it's easy to read, less typing and it nearly tied as the fastest method:

我喜欢使用以下语法来检查,因为它易于阅读,更少键入,并且它几乎是最快的方法:

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

To assign a value back to that attribute when you don't know the name of it before hand use the attribute method:

要在手动使用属性方法之前不知道其名称时将值赋值回该属性:

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";
}

All of the following are ways to test if an attribute exists gathered from the answers listed on this question. Since there were so many I ran each in the 11.7.0.225 debug player. The value on the right is the method used. The value on the left is the lowest time in milliseconds it takes when running the code one million times. Here are the results:

以下所有方法都是测试是否存在从此问题中列出的答案中收集的属性的方法。因为我在11.7.0.225调试播放器中运行了很多。右边的值是使用的方法。左边的值是运行代码一百万次时所用的最短时间(以毫秒为单位)。结果如下:

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

Performance Test code:

性能测试代码:

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 != item.@["style"]);
    attExists = (undefined != item.@["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 = item.@style[0];
    attExists = item.@value[0];
}

var test5:int = getTimer() - time; // 1838 1756 1756 1775

#6


1  

I have figured out a solution, I'm still interested if there is a better way to do this...

我找到了一个解决方案,如果有更好的方法,我仍然感兴趣......

This will work:

这将有效:


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

#7


0  

Here you go:

干得好:

if(event.result.@error[0]){
    //exists 
}

Easy, eh? :)

容易,嗯? :)