This post is a brief note on how to use reserved words like “id” and “class” with e4x in flex.

Basically you have reserved keywords in flex like “id” and “class” amongst others… I was traversing through some xhtml that i brought in remotely and it had many div tags. One of the issues I ran into with it was trying to manipulate div’s with certain class attributes… so i was doing some searching through the html with e4x and trying to grab things with e4x operators like body..div.(@class == ‘headlineText’) this would theoretically grab all of my div tags that have that class name…

but the compiler bombs on this and says class is reserved… so the proper syntax for this is body..div.(@[‘class’] == ‘headline’) I found this when looking at some xml standards online
NOTE: you may get an error: ‘A term is undefined and has no properties’ error when searching for divs that don’t have the class attribute… you pretty much have use a for each loop and loop through the div XMLList that you would get back from these operations:

var body:XML = myXML.body[0];
var divList:XMLList = body..div; //searchs body for all div tags

then you would loop through them.

for each( var p:XML in divList )
if( p.@[‘class’] == ‘headlineText’ )
//do something

for some reason you get an error when trying to filter this way with xml when the attribute isn’t there… this only seems to happen when you use filtering operations like body..div (which gets all of the div’s) I think it may actually be a bug… it seems to me this should just return an empty XMLList if there isn’t any found… but i guess it just doesnt work…. hmmm… anyway

thats all for now.