March 9, 2009

ActionScript 3: Checking for empty xml nodes

When parsing xml in AS3, you grab a leaf node's inner content by using the XML( node ).text() function. You can compare the result with a String, but if you're checking for an empty string, the .text() function doesn't compute, because it returns an XMLList, instead of a String. While a non-empty string comparison works fine between an XMLList and a String, an empty String ( i.e. var mystring:String = ""; ) is not the same as an empty XMLList. To ensure that your comparison works, it's always a good policy to cast your text node to a String. See the example below:

var isEmptyNode:Boolean = false;
if( String( node["myNodeName"].text() ) == "" )
isEmptyNode = true;

1 comment: