2 Replies Latest reply on Dec 8, 2006 2:41 AM by Julio Carneiro

    XML string to ArrayCollection, how to?

    Julio Carneiro Level 1
      Hi all,

      I'm a beginner Flex 2 and having trouble populating a PopUpMenuButton.

      The data for the menu comes from a webservice as an xml string:
      <Protocol>
      <record>
      <id>1</id>
      <name>first</name>
      </record>
      <record>
      <id>2</id>
      <name>second</name>
      </record>
      ...
      </Protocol>

      Popup menu button is defined as:
      <mx:PopUpMenuButton id="addMenu" label="Add" dataProvider="{protocolList}" labelField="name" showRoot="false" itemClick="runAddMenu(event);"/>

      Result event handler is:
      [Bindable] private var protocolList:XML;
      private function gotIt(e:ResultEvent):void{
      protocolList = new XML(e.result);
      }

      With that setup the popup shows up somewhat like:
      Add >
      first >
      1 >
      1
      first >
      first
      second >
      2 >
      2
      second >
      second

      Repeating values where I want just a straight menu.
      I am not using attributes but the node element data/value and that seems to cause confusion.

      I figured that the way to fix it is to convert the XML data into an ArrayCollection, but I cannot get that to work.

      From the documentation and examples I assumed that'd be as simple as changing my Result handler to:
      [Bindable] private var protocolList:ArrayCollection;
      private function gotIt(e:ResultEvent):void{
      var temp:XML = new XML(e.result);
      protocolList = temp.Protocol.record;
      }

      Problem is that gives me a runtime error: unable to coerce XMLList to ArrayCollection.

      For some reason "temp" gets typed as XMLList and not XML and I could not find a way to make it into XML.

      I know I can traverse the xml tree and build the ArrayCollection, but it seems overkill as Flex should be able to do it for me.

      Am I overseeing something obvious here?

      TIA
      Julio Carneiro
        • 1. Re: XML string to ArrayCollection, how to?
          ntsiii Level 3
          Well, to get the XMLList into the ArrayCollection, do:
          protocolList = new ArrayCollection(temp.Protocol.record);

          I'm not sure that will solve everything though.

          Before you do that, try:
          protocolList = new XML(e.result).record;

          Tracy
          • 2. Re: XML string to ArrayCollection, how to?
            Julio Carneiro Level 1
            Thanks Tracy, but I ended up iterating over the XML data to generate an Array to use on the menu. I had tried your suggestions before but the result was always a hierarchical menu, where it should be flat. Possibly a problem with using element values instead of attributes.

            In the end it was really simple to do it this way, here is my final code it it helps someone else:

            [Bindable] private var protocolList:Array;
            private function loadProtocolsComplete(e:ResultEvent):void{
            var temp:XML = new XML(e.result);
            protocolList= new Array({name:"Select", id:-1});
            for each (var elem:XML in temp.record) {
            protocolList.push({name:elem.name, id:elem.id});
            }
            addMenu.dataProvider=protocolList;
            }