3 Replies Latest reply on Mar 7, 2008 1:19 PM by ntsiii

    Creating a list

    maperr
      Hello all! Sorry the basic nature of this post. I've been working with Flex for two days now and am stuck trying to generate a list of values. I have written several Java classes that produce xml like this

      <?xml version="1.0" encoding="utf-8" ?>
      - <data>
      - <metric name="AccRate">
      <entry year="2001" value="0.115471303" />
      <entry year="2002" value="533.0" />
      <entry year="2003" value="143.0" />
      <entry year="2007" value="0.0" />
      </metric>
      - <metric name="AccRateEx">
      <entry year="2001" value="0.115057528" />
      <entry year="2002" value="0.20059371" />
      <entry year="2003" value="0.055949867" />
      <entry year="2007" value="0.0" />
      </metric>
      - <metric name="AccRateNonex">
      <entry year="2001" value="0.119266033" />
      <entry year="2002" value="0.223484755" />
      <entry year="2003" value="0.034090906" />
      <entry year="2007" value="0.0" />
      </metric>
      </data>

      My goal is to iterate over this data and place the metric name value in an array. Here is what I have done thus far.

      var metrics: Array;
      function serviceResult() {
      metrics=new Array();
      for (var i in srv.lastResult.data.metric[0]) {
      if (i!="name") {
      metrics = {srv.name};//Not sure what to do here
      }
      }
      metrics.reverse();
      }


      <mx:HTTPService id="srv"
      url=" http://localhost:8080/Flash/flashdata/metric_values.jsp"
      result="serviceResult()"/>


      <mx:List id="list" dataProvider="{metrics}" width="130" height="100%" dragEnabled="true"/>


      Like I said, I know this is probably basic, but the several examples I've come across do not work in Flex Builder 3.

      Any help is greatly appreciated.

      Thanks!
      Matt
        • 1. Re: Creating a list
          SujitG Level 2
          Hi,
          Use e4x approach of reading XML. Set your HTTPService component's resultFormat to "e4x" and convert your srv.lastResult to XML using the "as" operator. Please find more details on how to read and write XML in the URL below.
          Working with e4x XML
          Hope this helps
          • 2. Re: Creating a list
            ntsiii Level 3
            Further, you are using a result handler. That is good. But always include the event in the declaration:
            result="serviceResult(event)" .../>

            As SujitG says, ALWAYS set resultFormat="e4x" There are few if any benefits to leaving the default.

            In your handler, verify your xml:
            private function serviceResult(event:ResultEvent):void
            var xmlResult:XML; //I usually declare this as an instance var, not local
            xmlResult = event.result as XML; //event is a plain object, do the cast
            trace(xmlResult.toXMLString); //make sure you have what you expect
            var xlMetrics:XMLList = xmlResult.metric; //returns an xmllist of metric nodes
            //loop over the XMLlist
            var xmlMetric:XML;
            for (var i:int=0;i<xlMetrics.length();i++) {
            xmlMetric = xlMetrics ; ref to curent metric node
            metrics.push(xmlMetric.@name ); //puts the value of the name attr in the array
            }
            trace(metric.toString())

            Tracy
            • 3. Re: Creating a list
              ntsiii Level 3
              The forum mangles array brackets:
              xmlMetric = xlMetrics [ i ]; ref to curent metric node