Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In actionscript, what's the best way to check if a xml node property exists?

If I have some xml like so:

<books>
    <book title="this is great" hasCover="true" />
    <book title="this is not so great" />
</books>

What's the best (or accepted) way in actionscript to check if the hasCover attribute exists before writing some code against it?

like image 831
Paul Mignard Avatar asked Nov 29 '22 12:11

Paul Mignard


1 Answers

Just to add some precisions.

If you want to check if the property exists even though it's empty you should definitely use hasOwnProperty :

var propertyExists:Boolean = node.hasOwnProperty('@hasCover');

Checking the length of the content is somehow dirty and will return false if the value of the attribute is empty. You may even have a run-time error thrown as you will try to access a property(length) on a null object (hasCover) in case the attribute doesn't exist.

If you want to test if the property exists and the value is set you should try both starting with the hasOwnProperty so that the value test (eventual run-time error) gets ignored in case the attribute doesn't exist :

var propertyExistsAndContainsValue:Boolean = (node.hasOwnProperty('@hasCover') && [email protected]());
like image 193
Theo.T Avatar answered Dec 09 '22 10:12

Theo.T