Flex safely checking if XML attribute exists

I’ve often faced the issue of having to use an XML attribute that may not always be present. I wanted to ensure I don’t receive any NullPointerExceptions trying to parse an attribute that’s not there, so here’s a quick and easy way to check.


var xmlNode:XML = <node attr1="this is a test"/>
if (String(xmlNode.@attr1).length > 0)
trace("Attr1 exists");
else
trace("No Attr1 exists");

Advertisements

One thought on “Flex safely checking if XML attribute exists

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s