Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 641
Boundless Avatar asked Jan 21 '12 00:01

Boundless


2 Answers

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!

like image 93
Jonathan Dunlap Avatar answered Sep 20 '22 17:09

Jonathan Dunlap


I found 4 ways:

if ('@id' in xmlIn)
if (xmlIn.hasOwnProperty("@id"))
if ([email protected]() > 0)
if (xmlIn.attribute("id").length() > 0)

and I prefere first method:

if ('@id' in xmlIn) 
{
    foo(xmlIn.@id);
}
like image 39
marbel82 Avatar answered Sep 20 '22 17:09

marbel82