3 Replies Latest reply on Jul 24, 2007 2:06 PM by Jihaes

    Generate items of a list from an XML file

    Jihaes Level 1
      if i have an xml file called temp.xml like
      <object id = "Item#1" data = "Something1">
      <object id = "Item#2" data = "Something2">
      <object id = "Item#3" data = "Something3">
      <object id = "Item#4" data = "Something4">

      and in my flex application, i have a <mx:List .../> what's the best way to populate the list items from the xml file?
      thanks
        • 1. Re: Generate items of a list from an XML file
          ntsiii Level 3
          Are you asking how to get the xml into the Flex app, or how to display it in a list?
          Tracy
          • 2. Re: Generate items of a list from an XML file
            Jihaes Level 1
            Display in a list.
            • 3. Re: Generate items of a list from an XML file
              Jihaes Level 1
              I guess I wasnt too clear but I found the answer to what I was looking for.

              In my list i can set the dataProvider to the array listItems and call my method loadmyXML()

              <mx:List dataProvider="{listItems}" id="myList" themeColor="#80ff00" top="20" width="100%" height="90%" labelFunction="loadmyXML)">


              <mx:Script>
              <![CDATA[
              import mx.collections.ArrayCollection;

              public var listItems:ArrayCollection = new ArrayCollection();

              public function loadmyXML():void{
              var loader:URLLoader = new URLLoader();
              loader.addEventListener(Event.COMPLETE, handleComplete);
              loader.load(new URLRequest ("some.xml"));
              }

              private function handleComplete(event:Event):void{
              try{
              var toolsXML:XML = new XML(event.target.data);
              //this will just output the xml in to the console
              trace(toolsXML);
              for each(var items:XML in toolsXML.elements())
              {
              //add each element from the xml to the array
              var something:Object = new Object();
              something.label = items;
              listItems.addItem(something);
              }
              }
              catch(e:TypeError)
              {
              trace("Could not parse text into XML");
              trace(e.message);
              }
              }
              ]]>
              </mx:Script>