4 Replies Latest reply on Jun 3, 2009 3:22 AM by Celinio

    HTTPService: request on a tag/field

    Celinio Level 1

      Hi,

      I have the following XML file that I use as a data source :

       

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

           <ville>
                <nom>Paris</nom>
                <region>Ile-de-France </region>
                <departement>Paris</departement>
                <codePostal>75001</codePostal>

           </ville>
           
           <ville>
                <nom>Bordeaux</nom>
                <region>Aquitaine</region>
                <departement>Gironde</departement>
                <codePostal>33000</codePostal>
           
           </ville>

      </villes>

       

      I have a map of France, when a user clicks on Bordeaux, I want to display the region, that is Aquitaine, in a panel.

      How do i request this XML file on a field (tag) ?

      In SQL, it would be something like that :
      Select region from ville where nom="Bordeaux" ==> Aquitaine

      In my code, I already have these lines  :

       

      ...
      <mx:HTTPService id="service" url="data/villes.xml" />
      ...
      <mx:DataGrid id="grid" dataProvider="{service.lastResult.villes.ville}" />
      ...

       

      Please not that the event which must display the region is not on the DataGrid.

      Thanks for helping.

        • 1. Re: HTTPService: request on a tag/field
          _Natasha_ Level 4

          Hi,

          it's easier to work with XML data. So you can save you XML data to variable or every time call lastResult from service.

          For geting data you want

          var Villes:XML = XML(service.lastResult.villes);

          var regions:XMLList = villes.ville.(nom == "Bordeaux").region; // get all regions. if regions.length()>0 some data founded

          <mx:HTTPService id="service" url="data/villes.xml" resultFormat="e4x"/>
          
          
          • 2. Re: HTTPService: request on a tag/field
            Celinio Level 1

            Thanks a lot.

            Following your advice, I wrote this :

            <mx:HTTPService id="serviceE4X" url="data/villes.xml" showBusyCursor="true" resultFormat="e4x" />

             

            <mx:Script>
                    <![CDATA[
                   
                  
                    // returns the region of the city
                    public function recupererRegion(city:String):String {

                        var Villes:XMLList = XML(serviceE4X.lastResult).villes.ville;
                        var laregion : String = Villes.ville.(nom == "Bordeaux").region ;
                         return laregion;
                    }
                       
                    ]]>
                </mx:Script>

             

            When i call the recupererRegion() function, it does not return anything.

            It should return the region of Bordeaux, since it is hard-coded in the function (instead of city, which i will do when it is working).

             

            I am not running this app in a web server, so there is no debug mode that would give me the opportunity to see what is in the Villes list.

            How can i do that ?

            Thanks for helping.

            • 3. Re: HTTPService: request on a tag/field
              _Natasha_ Level 4

              As I remember this function don't work with XMLList.

               

              // returns the region of the city
                      public function recupererRegion(city:String):String {

               

                          var Villes:XML = XML(serviceE4X.lastResult);

                        Alert.show(Villes.toXMLString()); // you can debug this way. check that xml is correct

                        var laregions : XMLList = Villes.ville.(nom == "Bordeaux").region;

                        if (laregions.lenght() > 0)
                          return laregions[0].text() ;

                        else
                           return null;
                      }

               

               

              Something like this.

              Hope it's helpful.

              • 4. Re: HTTPService: request on a tag/field
                Celinio Level 1

                I found it :

                    var lesVilles:XML = XML(serviceE4X.lastResult);
                     var laregion : String = lesVilles.ville.(nom == city).region ;

                 

                Thanks a lot.