3 Replies Latest reply on Nov 7, 2011 10:16 PM by Flex harUI

    Flex e4x XML can't figure out?

    Log Home

      I'm trying to figure out e4x and flex.  Normally I use Connect to Data / Serices, but in this particular case, the XML is set up more nested.  I'm trying to build a road conditions app for Colorado, here is the XML URL:

       

      http://www.cotrip.org/xml/cameras.xml

       

      Why am I not able to see the 'ImageLocation' in my List?  When I put a breakpoint at the end of the last trace statement I get the data, but how do I display this data in a list?

       

      <?xml version="1.0" encoding="utf-8"?>

      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                     xmlns:s="library://ns.adobe.com/flex/spark"

                     creationComplete="handleCreationComplete(event)">

          <fx:Declarations>

              <!-- Place non-visual elements (e.g., services, value objects) here -->

              <s:HTTPService id="imageService"

                             url="http://www.cotrip.org/xml/cameras.xml"

                             resultFormat="e4x"

                             result="handleImageResult(event)"/>

             

          </fx:Declarations>

       

          <fx:Script>

              <![CDATA[

                  import mx.collections.XMLListCollection;

                  import mx.events.FlexEvent;

                  import mx.rpc.events.ResultEvent;

                 

                  [Bindable]

                  private var ImageLocation:XMLListCollection;

       

                 

                 

                  private function handleCreationComplete(event:FlexEvent):void {

                      imageService.send();

                  }

       

                  private function handleImageResult(event:ResultEvent):void {

                 

                      var cameraXML:XML = event.result as XML;

                      var cameraNS:Namespace = cameraXML.namespace("camera");

                      var cameraList:XMLList = cameraXML.cameraNS::Camera;

                      for each (var camera:XML in cameraList) {

                          trace("id: " + camera.cameraNS::CameraId);

                          var views:XMLList = camera.cameraNS::CameraView;

                          for each (var view:XML in views) {

                              trace("ImageLocation: " + view.cameraNS::ImageLocation);

                          }

                      }

                  }

              ]]>

          </fx:Script>

       

              <s:List left="200" height="40" dataProvider="{imageService}"/>

                 

      </s:Application>

        • 1. Re: Flex e4x XML can't figure out?
          Flex harUI Adobe Employee

          If myXML is a variable referencing the XML and the camera namespace is “open”, just try

           

              myXML..ImageLocation

          • 2. Re: Flex e4x XML can't figure out?
            Log Home Level 1

            I guess that gets me 1/2 way there, but now, how do I make myXML a variable feferencing the XML?

            • 3. Re: Flex e4x XML can't figure out?
              Flex harUI Adobe Employee

              Sorry, I didn’t see that you posted code.  I think the code needs to look like:

               

              private function handleImageResult(event:ResultEvent):void {
              
                              var cameraXML:XML = event.result as XML;
                              var cameraNS:Namespace = cameraXML.namespace("camera");
                              imageLocation = new XMLListCollection(cameraXML..cameraNS::ImageLocation);
              

               

              The list should be bound to imageLocation and not imageService.

               

              And you may need a labelFunction that looks like:

               

                  private function xmlLabelFunction(data:Object):String

                  {

                      return data.text();

                  }