5 Replies Latest reply on Mar 31, 2010 1:44 PM by Peter deHaan

    from XML to mx.control.List

    JPAntona
      var x:XML;
      x = <data><employees><employee label="A, Jean-Paul" data="23511"/>
                <employee label="B, John" data="12345"/>
                <employee label="C, John" data="23456"/>
                <employee label="D, Kelly" data="34567"/>
                 <employee label="E, Martin" data="45678"/>
                <employee label="F, Stacey" data="56789"/>
                <employee label="G, Tracy" data="67890"/>
      </employees></data>;
      
        • 1. Re: from XML to mx.control.List
          Peter deHaan Level 4

          Not exactly sure what your question is, but you can display that XML structure in an mx:List control using the following code:

           

          function(){return A.apply(null,[this].concat($A(arguments)))}

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
             
              <mx:Script>
                  <![CDATA[
                      private function init():void {
                          var theXML:XML;
                          theXML = <data><employees><employee label="A, Jean-Paul" data="23511"/>
                                    <employee label="B, John" data="12345"/>
                                    <employee label="C, John" data="23456"/>
                                    <employee label="D, Kelly" data="34567"/>
                                     <employee label="E, Martin" data="45678"/>
                                    <employee label="F, Stacey" data="56789"/>
                                    <employee label="G, Tracy" data="67890"/>
                          </employees></data>;
                          lst.dataProvider = theXML..employee;
                      }
                     
                  ]]>
              </mx:Script>
             
              <mx:List id="lst" labelField="@label" width="100" initialize="init();" />
             
          </mx:Application>

           

          Peter

          1 person found this helpful
          • 2. Re: from XML to mx.control.List
            JPAntona Level 1

            You were able to interpret my question from that broken post (I accidentally hit post before I was ready). You deserve credit for that.

             

            I applied your post to my code (see below) put it still ain't working. This is Flex 3 in Flex Builder.

             

            I am having difficulty getting XML  attribute values to display as  items in a mx.controls.List. The  context for the code below is a  ActionScript class definition.

            public function getSuggestions(evt:KeyboardEvent):void {

            var x:XML;
            x = <data><employees><employee label="A, Jean-Paul" data="23511"/>
                      <employee label="B, John" data="12345"/>
                      <employee label="C, John" data="23456"/>
                      <employee label="D, Kelly" data="34567"/>
                       <employee label="E, Martin" data="45678"/>
                      <employee label="F, Stacey" data="56789"/>
                      <employee label="G, Tracy" data="67890"/>
                 </employees></data>;

                 //Alert.show(x.toString());
                                //var lxml:XMLListCollection = new XMLListCollection(x.employees);
                                //theList.labelFunction=xmlLabelParser;
                                //theList.labelField="@label";
                                //theList.dataProvider = lxml;
                                theList.dataProvider = x.employee;
                                theList.height = listHeight;
                                Alert.show("" + theList.rowCount); // this always print "5" no matter what
                                this.height = theText.height;
                                
                                theList.includeInLayout = true;
                                theList.visible = true

            }

            public function xmlLabelParser(item:Object):String {
                           Alert.show(xmlLabelParser: + item.toString());
                           var lxmx:XML = new XML(item);
                        return item.employees.employee.label;
                    }

            Here is the MXML:

            <mx:TextInput id="givenInput" x="1" y="1"
                           editable="true" enabled="true" width="100%" />

                      <mx:List height="100" enabled="true" x="0"
                            width="100%" visible="false"
                           includeInLayout="false" labelFunction="{xmlLabelParser}" />


             

            What  seems  to be happening is that the Collection is holdint just one object  -  the entire XML object. From the Alert.show in the label function I  can  see that the item is the entire XML contents. Even so, I can't seem  to  get any data out of it.

            • 3. Re: from XML to mx.control.List
              Peter deHaan Level 4

              OK, doing a bit of guessing here, but in your getSuggestions() method, change this...:

              function(){return A.apply(null,[this].concat($A(arguments)))}

              theList.dataProvider = x.employee;

               

              ... to this:

              function(){return A.apply(null,[this].concat($A(arguments)))}

              theList.dataProvider = x.employees.employee;

               

               

              Next guess would be to change your xmlLabelParser() method from this...:

              function(){return A.apply(null,[this].concat($A(arguments)))}

              return item.employees.employee.label;

               

              ... to this:

              function(){return A.apply(null,[this].concat($A(arguments)))}

              return lxmx.@label;

               

               

              Peter

              1 person found this helpful
              • 4. Re: from XML to mx.control.List
                JPAntona Level 1

                OK. It is working now. The key was this:

                 

                theList.dataProvider = x.employees.employee;

                • 5. Re: from XML to mx.control.List
                  Peter deHaan Level 4
                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
                      
                      <mx:Script>
                          <![CDATA[
                              public function getSuggestions():void {
                                  var x:XML;
                                  x = <data>
                                          <employees>
                                              <employee label="A, Jean-Paul" data="23511"/>
                                              <employee label="B, John" data="12345"/>
                                              <employee label="C, John" data="23456"/>
                                              <employee label="D, Kelly" data="34567"/>
                                              <employee label="E, Martin" data="45678"/>
                                              <employee label="F, Stacey" data="56789"/>
                                              <employee label="G, Tracy" data="67890"/>
                                          </employees>
                                      </data>;
                                  
                                  theList.dataProvider = x.employees.employee;
                              }
                              
                              public function xmlLabelParser(item:XML):String {
                                  return item.@label;
                              }
                          ]]>
                      </mx:Script>
                      
                      <mx:List id="theList" width="200" labelFunction="xmlLabelParser" initialize="getSuggestions();" />
                      
                  </mx:Application>
                  

                   

                  Peter