E4X in Actionscript 3 throwing variable not defined error

288 views Asked by At

I've been using E4X expressions and it has never caused a problem. Today I got this error which is driving me crazy. Checked everything thousand times, searched web, but nothing.

So this is my query for the value:

var objectName:String = myXML.objects.object.(@id==objectId);

where objectId is an int.

This is the part of my XML:

<objects>

    <object id="0">value 1</object>
    <object id="1">value 2</object>
    <object id="2">value 3</object>

</objects>

When the above line is run I get an error which seems like a total nonsense to me:

ReferenceError: Error #1065: Variable id is not defined.
1

There are 1 answers

4
fsbmain On BEST ANSWER

It's because not all of your object nodes has the id attribute, try more safety way to search through the attributes with hasOwnProperty("@id") checks:

    var objectName:String = 
        xml.objects.object.(hasOwnProperty("@id") && @id=="2");