Skip navigation
StevenSchaer
Currently Being Moderated

How to fill a combobox with an external XMl

Dec 28, 2012 7:33 AM

Tags: #flex #xml #external #flex4.5

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!

 
Replies
  • Currently Being Moderated
    Jan 1, 2013 9:06 PM   in reply to StevenSchaer

    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>

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 2, 2013 12:55 AM   in reply to StevenSchaer

       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

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 2, 2013 1:35 AM   in reply to Supreet R

    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>

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 2, 2013 4:36 AM   in reply to StevenSchaer

    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>

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points