Check if an xml attribute exists

February 2, 2011 by fMajakovskij

This is the best method I found so far to check is an xml attribute exists and have some value set.

You can use the following function to do it:

public function isSetXMLAttribute(__xml:XML, __attr:String):Boolean
{
	if(!__xml) return false;

	if(("@"+__attr) in __xml)
	{
		if(__xml.@[__attr].toString() == "") return false;

		return true;
	}
	return false;
}

Assuming you have the following xml object:

var xml:XML =
                            <item name="somename" />

Use the function like that to check the @name attribute:

var item:XML = xml[0];
var exists:Boolean = isSetXMLAttribute(item, "name");

That’s it.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>