2 Replies Latest reply on Jan 30, 2008 6:32 AM by fivebuck

    Using E4X issue

    fivebuck
      Hello,

      What I am trying to do is to show an Alert depending on if a certain attribute exists within my xml. I am getting the Alert to show up, but it is showing up for each selection I make in my comboBox(which shows formats from the xml) even though the attribute exists in only one of the nodes. Any ideas? Below is the code I have and the xml.

      MXML & ACTIONSCRIPT I'M USING:

      <mx:HTTPService id="formatData" url="../assets/formats.xml" resultFormat="e4x"/>

      <mx:Script>
      <![CDATA[
      import mx.collections.*;
      import mx.rpc.events.*;
      import mx.controls.Alert;

      private function example(event:Event):void {
      var materialNodes = formatData.lastResult.format.material;

      if (materialNodes.(hasOwnProperty("@name") && @name == length())) {
      Alert.show("hello");
      }
      }

      ]]>
      </mx:Script>

      <mx:ComboBox width="120" dataProvider="{formatData.lastResult.format.@id}" id="formatComboBox" change="example(event)"/>

      THE XML I'M USING:

      <?xml version="1.0" encoding="iso-8859-1"?>

      <formats>
      <format id = "None" image = "images/image_icon.gif">
      </format>

      <format id = "Format 1" image = "images/format1_icon.gif">
      <material name = "materials">None</material>
      <material>Material 1</material>
      <material>Material 2</material>
      <material>Material 3</material>
      <material>Material 4</material>
      </format>

      <format id = "Format 2" image = "images/format2_icon.gif">
      </format>

      <format id = "Format 3" image = "images/format3_icon.gif">
      </format>

      <format id = "Format 4" image = "images/format4_icon.gif">
      </format>
      </formats>

      P.S. I have been told that I am 'querying' the materialNodes variable that has data from the service, rather than the currentTarget of the comboBox change event. And to first try tracing out event.currentTarget and see what you get out. I added the event.currentTarget trace statement to the example function and this is what I got back.

      ImprintUserInterface1_0.VBox4.formatsSelect29.VBox30.Canvas36.formatsVBox.formatComboBox

      It seems it's just the path to the comboBox. I must be referencing the event.currentTarget wrong.
      Any suggestions would be appreciated.
        • 1. Re: Using E4X issue
          ntsiii Level 3
          Yes, you should never use lastResult in script. It is intended for binding expressions only.

          Instead do:
          var xmlFormat:XML = XML(formatComboBox.selectedItem);
          trace(xmlFormat.toXMLString())

          The e4x expression is a bit tricky. I would think that this would work:
          var xlNodesWithName:XMLList = xmlFormat.material.(attribute("name").lenght()>0)

          But I recall a problem with this because atribute() returns an xmllist itself. Perhaps .text().length>0?

          also, the "in" operator might work here.

          Tracy
          • 2. Re: Using E4X issue
            fivebuck Level 1
            Hey Tracy,

            Figured it out. Below is what I ended up using with the help from a co-worker. Thank you for your reply.

            private function example():void {
            var myXML:XML = new XML(formatData.lastResult);
            var strFormat:String = formatComboBox.selectedLabel;
            var materialList:XMLList = myXML.format.(@id == strFormat).material;

            if (materialList.length() > 0) {
            Alert.show("hello");
            }
            }