3 Replies Latest reply on Jul 25, 2011 5:04 AM by Pooja Kuber

    e4x problem

    BriHami

      Hi

       

      I am trying to label a button from nodes in an xml file but end up having it labeled with the whole XML file. Have tried using toXMLSting() but with no joy. If anyone could help that would be great, the code is shown at the bottom.

       

      Also another problem I have. When I add buttons dynamically (through the functionchangeIt) they are all added in the same place, the top left hand corner, making them impossible to see. Is there a way to have the buttons add next to each other so the whole button can be seen.

       

      Many thanks for any help

       

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

      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()"

                              xmlns:mx1="library://ns.adobe.com/flex/mx">

       

           

            <mx:HTTPService url="http://www.w3schools.com/xml/cd_catalog.xml"

                        id="xmlService" resultFormat="e4x"

                        result="xmlObj = XML(xmlService.lastResult)"/>

            <mx:Script>

                  <![CDATA[

                        import mx.controls.Alert;

                        import mx.core.UIComponent;

                        import mx.rpc.events.ResultEvent;        

                        import spark.components.Button;

       

                  private var loader:URLLoader = new URLLoader();

                                         

                        [Bindable]

                        private var xmlObj:XML;

                        [Bindable]

                        private var xmlObj2:XMLList;

                        [Bindable]

                        private var value:String;

                        [Bindable]

                        private var value2:String;

                       

                        private function init():void{

                              loader.addEventListener(Event.COMPLETE, setResult);

                              xmlService.send()

                        }

                       

                        private function setResult(event:Event):void{

                              xmlObj = (loader.data as XML)

                        }

                       

                        private function navList():void{

                        //    Alert.show("in")

                              for each (value in xmlObj.CD.TITLE)

                              {

                                    var myInstance2:spark.components.Button = new spark.components.Button();

                                    myInstance2.label = value;

                                  myInstance2.addEventListener("click",changeIt);

                                    myInstance2.id=value;

                                    vb1.addChild(myInstance2);                           

                              }

                        }

                        private function changeIt(value:Event):void{

                              var myID:String = (value.currentTarget).id;

                              xmlObj2 = xmlObj.CD.(TITLE == myID);     

                              for each (value2 in xmlObj2.*){                      

                                    var gameOdds:spark.components.Button = new spark.components.Button();

                                    gameOdds.label = value2;

                                    gameDetails.addChild(gameOdds);

                              }    

                        }

                                         

                  ]]>

            </mx:Script>

            <mx:Button click="navList();" label="{xmlObj.CD.TITLE.toXMLString()}"/>

            <mx:Panel height="100%" id="vb1">

                       

            </mx:Panel>

           <mx:Button label="{value}"/>

            <mx1:TextArea text="{xmlObj2.*}"/>

            <mx:Panel width="250" height="200" layout="absolute" id="gameDetails">

            </mx:Panel>

           

      </mx:Application>

        • 1. Re: e4x problem
          saisri2k2 Level 4

          Please paste your xml here. try using XML.ignoreRootNode = true.

          • 2. Re: e4x problem
            EvyatarBH Level 3

            First of all, the Buttons you create are stacked on each other because you add them to a Panel, which uses an Absolute layout (x & y positioning).

            You can use an HBox instead.

             

            However, if you use Spark Button, why not using all Spark components (Application, TextArea etc.)?

            In that case, you could use HGroup or some container with HorizontalLayout...

            • 3. Re: e4x problem
              Pooja Kuber

              To solve your first problem use the childern() method, which will return all the TITLE names.

               

              I am trying to label a button from nodes in an xml file but end up 
              having it labeled with the whole XML file. Have tried using toXMLSting()
               but with no joy. 

               

                    <mx:Button click="navList();" label="{xmlObj.CD.TITLE.children()}"/>

               

              Thanks and Regards,
              ......................................................................................... ..
              Pooja Kuber | pkuber@infocepts.com | www.infocepts.com

              .......................................................................................... .