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");