2 Replies Latest reply on Apr 27, 2007 10:20 PM by thedex1

    Working with XML and Button

    thedex1
      Hi,

      How are all of you. Well I am new to Flex. But I have started building simple applications. One of the top most problem I am facing is working with XML and Button. Can you please assist me in this. I am explaining my problem:

      I have an external XML file like this:
      <Menu>
      <button>
      <idnt>0</idnt>
      <label>General Health</label>
      <text>General Health pages is currently under construction</text>
      </button>
      <button>
      <idnt>1</idnt>
      <label>Mental Health</label>
      <text>Mental Health pages is currently under construction</text>
      </button>
      </Menu>

      Now I want to generate Buttons Dynamically from this XML. And the second thing which is the most problematic is that how I code it so that when I press the Button labled "General Health", it will show the same text as in the XML tag coresponding to tag "<label>General Health</label>" ?
      I badly need this. I am realy confused on this. Kindly help me.

      Regards
      ..::DeX
        • 1. Re: Working with XML and Button
          peterent Level 2
          Let's assume that variable "node" contains one element of the XML. For example,
          <button>
          <idnt>0</idnt>
          <label>General Health</label>
          <text>General Health pages is currently under construction</text>
          </button>

          such that node.label would be "General Health", node.idnt would be 0, etc.

          You can build a Button like this:

          var b:Button = new Button();
          b.label = node.label;
          b.data = node; // more on this later
          b.width = 60;
          b.height = 26;
          addChild(b); // critical - adds the button to the display list so you can see it
          b.addEventHandler( MouseEvent.CLICK, handleClick );

          You must set the button's width and height unless the button will be in a container that will size its own children (like Tile).

          Every Flex component has a data property. You can set it to whatever you like. For your needs it makes sense to set each Button's data property to the node it relates to.

          Now suppose that code above is in a function, createButton:

          private function createButton( node:XML ) : void {
          // code from above
          }

          Here's how to make all the buttons where "menu" is a variable that contains your XML:

          for(var k:int=0; k < menu.button; k++) { // menu.button is an XMLList
          createButton( menu.button[k] );
          }

          Now to handle the event:

          private function handleEvent( event:MouseEvent ) : void
          {
          var b:Button = event.currentTarget as Button;
          trace( b.data.text);
          }

          When a button is picked, the description element will print in the debug console. Replace the trace with whatever code you need.
          • 2. Re: Working with XML and Button
            thedex1 Level 1
            Dear Peter,

            Thank you, thank you very much...bundles of thanks from me. You really help me with this. I was not expecting such a detailed response from anyone. But you proved it wrong. Thank you very very much.
            My MSN account ID is "de.xsense@hotmail.com". I would greatly appreciate if either you add me to your contact list or let me know your email address. I am not asking your hotmail ID to ask you a lot of questions, just would like to be a friend to share new ideas and knowledge.

            Thank again...
            ..::DW