Friday, March 5, 2010

AS3: Does my xml doc contain a attribute

This is a simple check which increases the ease of using xml.

if you have a xml file like so:

<root_node some_attribute="">
</root_node>

in AS3 you can decide if a attribute exist by using the lenght() method. So code using the xml would be:

var my_xml:XML = some_function_to_get_xml();

if( my_xml.@some_attribute.length() != 0 ) {
trace("Yup, some_attribute exists");
}

if( my_xml.@some_non_existant_attribute.length() != 0) {
trace("This will not be printed since attribute does not exist");
}

Note it is easy to make a mistake and forget to use length as a method i.e. "length()" and use it as variable i.e. just "length" which will not work.

No comments:

Post a Comment