Alex Rivera | Logout

How can I test if an attribute has been set in an XML node using AS3

Asked 2012-01-21T00:37:18.097
9

I am reading in an XML file in AS3. I need to find out if an attribute exists on a node. I want to do something like:

if(xmlIn.attribute("id")){
foo(xmlIn.attribute("id"); // xmlIn is of type XML
}

This doesn't work however. The above if statement is always true, even if the attribute id isn't on the node.

Edit
Report

1 Answer

18

You have to do this instead:

if(xmlIn.hasOwnProperty("@id")){
    foo(xmlIn.attribute("id"); // xmlIn is of type XML
}

In the XML E4X parsing, you have to use hasOwnProperty to check if a property for the attribute as been set on the E4X XML object node. Hope this helps!

answered 2012-01-21T00:54:48.790

Your Answer