3 Replies Latest reply on Nov 9, 2007 7:45 PM by ntsiii

    XML Traversing

    vij010
      Hi,
      I have this following XML file....

      <?xml version="1.0" encoding="utf-8" ?>
      <CarsService>
      <make name ="Acura">
      <model name="Intergra">
      <color name ="Green"/>
      <color name ="White"/>
      <color name ="Pale Blue"/>
      <color name ="Fiery Yellow"/>
      <color name ="Royal Blue"/>
      </model>
      <model name="Reva GTX-I">
      <color name ="Orange"/>
      <color name ="OffPink"/>
      <color name ="Chesty Brown"/>
      <color name ="Limestone"/>
      <color name ="Phantom Gray"/>
      </model>
      <model name="Slipshack">
      <color name ="Cold Blue"/>
      <color name ="10:20"/>
      <color name ="Sky Blue"/>
      <color name ="Royal Pink"/>
      <color name ="Royal Blue"/>
      </model>
      </make>
      <make name ="Mercedez Benz">
      <model name="Compressor S Class">
      <color name ="Flaming Orange"/>
      <color name ="Angelic White"/>
      <color name ="Muddy Brown"/>
      <color name ="Black"/>
      <color name ="Turquoise"/>
      </model>
      <model name="S 280">
      <color name ="Pale Green"/>
      <color name ="Pale White"/>
      <color name ="Light Yellow"/>
      <color name ="Emerald"/>
      <color name ="Silver"/>
      </model>
      <model name="s380">
      <color name ="Sliver and gold"/>
      <color name ="10:20"/>
      <color name ="Sky Blue"/>
      <color name ="Cool Blue"/>
      </model>
      </make>
      <make name ="BMW">
      <model name="Z6">
      <color name ="Black"/>
      <color name ="Orange"/>
      <color name ="Gray"/>
      <color name ="Fiery Red"/>
      <color name ="Violet"/>
      </model>
      <model name="X5">
      <color name ="Brown"/>
      <color name ="Blue"/>
      <color name ="Flaming Red"/>
      <color name ="Yellow"/>
      </model>
      <model name="5 Series ">
      <color name ="White"/>
      <color name ="Yellow"/>
      <color name ="Green"/>
      <color name ="Black"/>
      </model>
      </make>
      </CarsService>

      and I have this following Flex Code....

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="srv.send()">
      <mx:HTTPService id="srv" url="carsservice.xml" resultFormat="e4x">
      </mx:HTTPService>

      <mx:Panel width="708" height="608" x="20" y="10" layout="absolute">
      <mx:DataGrid x="10" y="10" width="668" id="myproducts" dataProvider="{srv.lastResult.make}">
      <mx:columns>
      <mx:DataGridColumn headerText="Car Name" dataField="Car Name"/>
      <mx:DataGridColumn headerText="Make" dataField="Make Name"/>
      <mx:DataGridColumn headerText="Color" dataField="Color"/>
      </mx:columns>
      </mx:DataGrid>
      </mx:Panel>
      </mx:Application>

      I don't get any results because I am not sure of travelling the XML nodes correctly. How to go about this ?, All I want to do is to load the XML data under the column headers : Car Name, Make Name, Color. I have tried all ways...This works on a tree view control though....

      Is this XML file correct ?...am sorry, am new to XML...

        • 1. Re: XML Traversing
          batmitra Level 1
          hi
          you have 2 great mistakes in your code:

          1st -if you to bind it to a datagrid you must use dataProvider="{srv.lastResult}" that should do the work, on a tree component besides telling the dataprovider you must sat wich attribute (labelfield) must it show on tree, with datagrid you don't need to tell it to the data provider, only on your collumns.

          2nd - on your collumns the datafield must be the filed itself, i mean, if you have model name on your xml you must tell the collumn thar you field is Model and the attribute is name , so it must be make.@name the @ sign tells flex to look into an attribute.

          i think that should do it although i haven't tried .
          • 2. Re: XML Traversing
            vij010 Level 1
            Hi,

            BATMITRA : It doesn't work. I tried to follow your suggestions, but still there's no data on my columns. Can you please suggest me the correct way to do this ?, I don't understand why it is so tough.
            • 3. Re: XML Traversing
              ntsiii Level 3
              First, do not bind directly to lastResult because it is hard to debug. Instead, use a result handler. Below is some sample copde that will help.
              Tracy

              Sample code using HTTPService, e4x, handler function to populate a list item.
              Also shows usage of AsyncToken.

              The DataGrid tag:
              <mx:DataGrid id="dg" dataProvider="{_xlcMyListData}" .../>


              The HTTPService tag:
              <mx:HTTPService id="service" resultFormat="e4x" result="onResult(event)" fault="..../>

              Script block declaration:
              import mx.rpc.Events.ResultEvent;
              [Bindable]private var _xlcMyListData:XMLListCollection;

              Invoke send:
              var oRequest:Object = new Object();
              oRequest.Arg1 = "value1";
              var callToken:AsyncToken = service.send(oRequest);
              token.callId = "myQuery1";

              Result Handler function:
              private function onResult(oEvent:ResultEvent):void {
              var xmlResult:XML = XML(event.result); //converts result Object to XML. can also use "as" operator
              var xlMyListData:XMLList = xmlResult.myListData; //depends on xml format, is row data
              _xlcMyListData = new XMLListCollection(xlMyListData); //wrap the XMLList in a collection
              trace(_xlcMyListData.toXMLString()); //so you can see exactly how to specify dataField or build labelFunction
              var callToken:AsyncToken = oEvent.token;
              var sCallId = callToken.callId; //"myQuery1"
              switch(sCallId) { //Process the result conditionally
              case "myQuery1":
              doQuery2(); //do whatever. this example calls another data service query
              break;
              ...
              }
              }//onResult