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.
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!
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With