7 Replies Latest reply on Aug 30, 2009 10:09 PM by manofspirit

    E4X with variables ?

    manofspirit Level 1

      Hello guys

       

      lets suppose i have an xml as following

       

       

      var xml:XML =
      
      <data>
           <boys>
                <boy name="john"/>
                <boy name="smith"/>
           </boys>
           <girls>
                <girl name="sara"/>
                <girl name="maya"/>
           </girls>
      </data>
      

       

      i can do like this

       

      var guys:XMLList = xml..boys.*;

      var gals:XMLList = xml..girls.*;

       

       

       

      How can i do the samething with a given variable like

      var choice:String = "boys";

       

      var guys:XMLList = xml..choice.*;

       

       

       

      Thanks in advance

        • 1. Re: E4X with variables ?
          MoonlightWare Level 1

          Try replacing

           

          var guys:XMLList = xml..choice.*;

           

          with

           

          var guys:XMLList = xml..[choice].*;

          • 2. Re: E4X with variables ?
            manofspirit Level 1

            Thanks for the reply

            But It raises compile time error.

            Any other way to do that?

            • 3. Re: E4X with variables ?
              Andrew Rosewarn Level 3

              Hi there

               

              I've made a slight change to the structure of your xml but this will achieve the result you want.  I hope this helps.

               

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

              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

              <mx:Script>

                   <![CDATA[   

                        private var xmlData:XML =

                            <people>

                                <person name='John'>

                                   <gender>Boy</gender>

                                    </person>

                                <person name='Smith'>

                                    <gender>Boy</gender>

                                </person>

                                <person name='Sarah'>

                                    <gender>Girl</gender>

                               </person>

                              <person name='Maya'>

                                    <gender>Girl</gender>

                               </person>

                            </people>;

               

               

                   private function handleSelection():void {

                        var choice:String = combo.selectedItem.toString();

                        var selectedXML:XMLList = xmlData.person.(gender == choice);

                   }

              ]]>

              </mx:Script>

               

                   <mx:ComboBox id="combo" x="77" y="74" dataProvider="['Boy','Girl']"></mx:ComboBox>

                   <mx:Button x="157" y="74" label="Select" click="handleSelection()"/>

               

              </mx:Application>

               

               

              Andrew

              1 person found this helpful
              • 4. Re: E4X with variables ?
                Andrew Rosewarn Level 3

                Or optionally a bit shorter on the xml front defining gender as an attribute and using the @ symbol in the e4X

                 

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

                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

                <mx:Script>

                     <![CDATA[  

                          private var xmlData:XML =

                              <people>

                                 <person name='John' gender='Boy'/>

                                 <person name='Smith'gender='Boy'/>

                                 <person name='Maya' gender= 'Girl'/>

                              </people>;

                 

                     private function handleSelection():void {

                          var choice:String = combo.selectedItem.toString();

                          var selectedXML:XMLList = xmlData.person.(@gender == choice);

                     }

                ]]>

                </mx:Script>

                 

                     <mx:ComboBox id="combo" x="77" y="74" dataProvider="['Boy','Girl']"></mx:ComboBox>

                     <mx:Button x="157" y="74" label="Select" click="handleSelection()"/>

                </mx:Application>

                 

                Andrew

                1 person found this helpful
                • 5. Re: E4X with variables ?
                  Barna Biro Level 3

                  @ Andrew: you're overcomplicating it without too much sense.

                   

                  package 
                  {
                      import flash.display.Sprite;
                  
                      public class XMLExample extends Sprite
                      {
                          private var _xml:XML = <data>
                                                      <boys>
                                                           <boy name="john"/>
                                                           <boy name="smith"/>
                                                      </boys>
                                                      <girls>
                                                           <girl name="sara"/>
                                                           <girl name="maya"/>
                                                      </girls>
                                                 </data>;
                          
                          public function XMLExample()
                          {
                              var choice:String = "boys";
                              var choiceXMLList:XMLList = _xml.child(choice).children();
                          }
                      }
                  }
                  

                   

                  Simply use the child(); and children(); methods to return the desired data.

                   

                  With best regards,

                  Barna Biro

                  • 6. Re: E4X with variables ?
                    Andrew Rosewarn Level 3

                    Barna

                     

                    Yep thats cool hadn't looked at the child / children methods.

                     

                    Much cleaner.

                     

                    Andrew

                    • 7. Re: E4X with variables ?
                      manofspirit Level 1

                      Thanks a lot Barna and Andrew