6 Replies Latest reply: Jan 2, 2013 8:42 AM by StevenSchaer RSS

    How to fill a combobox with an external XMl

    StevenSchaer

      Hi, as the title says it, how can you fill a combobox with an external xml..

      My XML is located in "data/kranten.xml" and looks like this:

       

      <kranten>

          <krant name="DeMorgen"    data="http://www.demorgen.be/nieuws/rss.xml" />

          <krant name="HLN"    data="http://www.hln.be/rss.xml" />

          <krant name="Nieuwsblad"    data="http://feeds.nieuwsblad.be/nieuws/snelnieuws" />

       

      </kranten>

       

      Can annyone supply me the mxml code ? I'm struggling for couple of hours already..

      Thanks in advance!

        • 1. Re: How to fill a combobox with an external XMl
          Supreet R

          Hello Steven,

           

          You can try the following:

           

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

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

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

                                 xmlns:mx="library://ns.adobe.com/flex/mx">   

              <fx:Script>

                  <![CDATA[

                      import mx.events.FlexEvent;

                     

                      private var kranten:XML = <kranten>

                                                  <krant name="DeMorgen" data="http://www.demorgen.be/nieuws/rss.xml" />

                                                  <krant name="HLN" data="http://www.hln.be/rss.xml" />

                                                  <krant name="Nieuwsblad" data="http://feeds.nieuwsblad.be/nieuws/snelnieuws" />

                                              </kranten>;

                     

                      protected function comboBox_initializeHandler(event:FlexEvent):void

                      {

                          xmlColl.source = kranten.children();

                      }

                  ]]>

              </fx:Script>

             

              <s:ComboBox id="comboBox" prompt="-Select-"

                          labelField="@name"

                          initialize="comboBox_initializeHandler(event)">

                  <s:dataProvider>

                      <s:XMLListCollection id="xmlColl"/>

                  </s:dataProvider>

              </s:ComboBox>

          </s:WindowedApplication>

          • 2. Re: How to fill a combobox with an external XMl
            ShardulSingh Community Member

               Use the Http service for this.Provide the localion of your .xml in the url of the httpService and get the enitre xml in the resultHandler of the http searvice.

             

             

             

             

             

            with Regards,

            Shardul

            • 3. Re: How to fill a combobox with an external XMl
              Supreet R Community Member

              Hello Steven,

                   Sorry for the above example with internal xml usage.

                   Following example uses the external xml to fill combobox, please take a look at it:

               

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

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

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

                                     xmlns:mx="library://ns.adobe.com/flex/mx">

                  <fx:Declarations>

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

                     <s:HTTPService id="service"

                                     url="/data/kranten.xml"

                                     fault="service_faultHandler(event)"

                                     result="service_resultHandler(event)"

                                     resultFormat="e4x"

                                     showBusyCursor="true"/>

               

                  </fx:Declarations>

               

                  <fx:Script>

                      <![CDATA[

                          import mx.controls.Alert;

                          import mx.events.FlexEvent;

                          import mx.rpc.events.FaultEvent;

                          import mx.rpc.events.ResultEvent;

               

                          protected function comboBox_initializeHandler(event:FlexEvent):void

                          {

                              service.send();

                          }

               

                          protected function service_faultHandler(event:FaultEvent):void

                          {

                              Alert.show(event.message.toString());

                          }

               

                          protected function service_resultHandler(event:ResultEvent):void

                          {

                              var kranten:XML = event.result as XML;

                              xmlColl.source = kranten.children();

                          }

               

                      ]]>

                  </fx:Script>

               

                  <s:ComboBox id="comboBox" prompt="-Select-"

                              labelField="@name"

                              initialize="comboBox_initializeHandler(event)">

                      <s:dataProvider>

                          <s:XMLListCollection id="xmlColl"/>

                      </s:dataProvider>

                  </s:ComboBox>

              </s:WindowedApplication>

              • 4. Re: How to fill a combobox with an external XMl
                StevenSchaer Community Member

                Thanks supreet!

                But now I'm struggling how I can pass the "name" to a function because the following code does not work:

                When I debug and set marker in front of the function, "name" is "".

                 

                public function chooseSiteFeed(name:String):void{

                 

                }

                 

                <s:ComboBox id="comboBox" prompt="-Select-"

                labelField="@name"

                 

                initialize="comboBox_initializeHandler(event)"

                change="chooseSiteFeed(ComboBox(event.target).selectedItem.name)"

                >

                 

                <s:dataProvider>

                 

                <s:XMLListCollection id="xmlColl"/>

                 

                </s:dataProvider>

                 

                </s:ComboBox>

                • 5. Re: How to fill a combobox with an external XMl
                  Supreet R Community Member

                  Hello Steven,

                   

                  Please try the following:

                   

                  public function chooseSiteFeed(name:String):void

                  {

                       trace(name);//displays the selectedItem

                  }

                   

                  <s:ComboBox id="comboBox"

                                       prompt="-Select-"

                                       labelField="@name"

                                       initialize="comboBox_initializeHandler(event)"

                                       change="chooseSiteFeed(ComboBox(event.target).selectedItem.@name)">

                       <s:dataProvider>

                            <s:XMLListCollection id="xmlColl"/>

                       </s:dataProvider>

                  </s:ComboBox>

                  • 6. Re: How to fill a combobox with an external XMl
                    StevenSchaer Community Member

                    Thanks for the help!