1 Reply Latest reply on Sep 19, 2007 9:54 AM by beyondpictures

    AS3.0 iterating through XML file

    beyondpictures
      I am new to AS3.0 and the new E4X way of working with XML. I'm trying to loop through a portion of an externally loaded XML file and display the results. Here is a portion of my XML file:
      <amenity id="1">
      <title>The Gate House</title>
      <feature>Majestic two-level Preserve Neighborhood Amenity Center</feature>
      <feature>Beautiful Gardens and Meandering Pathways</feature>
      <feature>Meeting Activity Room, Theatre Room, Gourmet Kitchen, Hearth Room, Grand Fireplace</feature>
      <feature>Decks and Patios</feature>
      <feature>Available to residents for parties, reunions, weddings, and meetings</feature>
      </amenity>

      The are other amenities as well. When a user clicks a button, I want to enumerate all of the features from this particular amenity. Here is my code to do this (the code associated with the listener for the button):

      function btnClick(eventObject) {
      var mc:MovieClip=eventObject.target
      var num:String = mc.name.replace("btn","")
      var xmlAmenity:XMLList = myXML.amenity.(@id==num)
      for each (var prop:XML in xmlAmenity) {
      trace (prop.feature);
      }
      }
      Basically the button that calls this is btn1, so the num variable ends up being 1. myXML is a global variable into which the external XML file has already been loaded. It then uses the num variable to assign the amenity with an id if 1 to the xmlAmenity variable as an XMLList. Everything works just grand except for the fact that the trace produces the following:
      <feature>Majestic two-level Preserve Neighborhood Amenity Center</feature>
      <feature>Beautiful Gardens and Meandering Pathways</feature>
      <feature>Meeting Activity Room, Theatre Room, Gourmet Kitchen, Hearth Room, Grand Fireplace</feature>
      <feature>Decks and Patios</feature>
      <feature>Available to residents for parties, reunions, weddings, and meetings</feature>

      I don't want the tags, just what's inside them. Is there some property I need to use (something like prop.feature.contents) or am I going about this the wrong way entirely? I suppose I could restructure my XML file to put the contents of the feature tag in an attribute called description and then trace the attribute with prop.feature.@description, but I'd kind of like to know how to access the text inside the open and close of the tag. If I access one directly (something like myXML.amenity.(@id==num).feature[0]) then it works just fine, I just can't seem to figure it out in a loop. I suppose I could increment a variable and do something like trace(myXML.amenity.(@id==num).feature[x]), but there must be a better way.