6 Replies Latest reply on Aug 19, 2008 9:39 AM by amthekkel

    A Simple XMLListCollection Question

    January_jsd
      Hi All,

      I have an XMLListCollection object (with XML inside ;) ) - I need to extract a single piece of XML from this object, and put it into a Label object. Try as I may (and for many hours), I can't achieve this - the closest I've come is to have the name of the XML branch printed in the Label object...

      Help, this is turning my hair gray ... :(
        • 1. Re: A Simple XMLListCollection Question
          amthekkel Level 1
          hi,
          try this, not sure if this is the best way to do it but might help you with your problem.
          make sure to import IViewCursor and XMLListCollection in your xml file.
          Lets say your XMLlistcollection is called XMLList1 and the following xml
          <xml1>
          <XMLElement_ID> 1<XMLElement_ID/>
          <XMLElementName>test1 <XMLElementName/>
          ..
          .
          </xml1>
          and a few other similar elements..
          create a bindable object e.g public var listIdMap:Object;
          Create a function and add the create the following:
          public function createObjectMap( ):void
          listIdMap = {};
          var listCursor:IViewCursor =XMLList1.createCursor();
          while (!listCursor.afterLast) {
          var datalist:XML = XML(listCursor.current);
          listIdMap[dataList.XmlElement_ID] = dataList;
          listCursor.moveNext();
          }

          then to access the element you want you can use the following
          listIdMap{XMLElement_ID].XMLElementName

          so if you do listIdMap[1].XMLElementName you should get "test1". You can store this into a bindable variable and set your label text bound to this variable.

          hope that helps.

          regards
          abhi
          • 2. A Simple XMLListCollection Question
            January_jsd Level 1
            Hi,

            Many thanks for the quick reply amthekkel - I mostly understand the code (and the rest I can research), and the compiler accepts it without error, but, I can't get the [bindable] / binding working.

            Essentially, I need a bound variable that fires an event when changed (see below code). My code never fires that event (or if it does, the event code is never executed).

            Sorry - I've only been learning Flex3 for a few weeks now ;)

            EDIT: everything that is executed is done so at creationComplete - not sure if that makes a difference.
            • 3. Re: A Simple XMLListCollection Question
              Level 7

              "January_jsd" <webforumsuser@macromedia.com> wrote in message
              news:g8ecdk$389$1@forums.macromedia.com...
              > Hi,
              >
              > Many thanks for the quick reply amthekkel - I mostly understand the code
              > (and
              > the rest I can research), and the compiler accepts it without error, but,
              > I
              > can't get the [bindable] / binding working.
              >
              > Essentially, I need a bound variable that fires an event when changed (see
              > below code). My code never fires that event (or if it does, the event
              > code is
              > never executed).
              >
              > Sorry - I've only been learning Flex3 for a few weeks now ;)
              >
              > [Bindable(event="UpdateMoney_Current")]
              > public var moneyCurrent:String;
              >
              > private function UpdateMoney_Current(e:Event):void {
              > // jsd: update the money label
              > donlab_current.text = moneyCurrent;
              > mountains.visible = false; // debug: test if the event is fired
              > }

              whenever you set moneyCurrent's value, you need to dispatch an
              UpdateMoney_Current event.

              Could you be a bit more specific about the end goal you're trying to
              accomplish, rather than telling us what your current approach to the problem
              is?


              • 4. Re: A Simple XMLListCollection Question
                amthekkel Level 1
                HI,
                I am new to flex as well. Not sure you can bind a variable like that, i could be wrong. If my understanding of your problem is correct then you want the label to have the updated value of moneyCurrent.

                the usual way i have seen this happen is that you have the moneyCurrent Bindable in script section of the file and then in your text or label component get the text property of that component bound to moneycurrent e.g
                <script>
                ..
                [Bindable]
                public var moneyCurrent:String;
                ...
                .
                </script>
                ..
                ..
                <mx: TextInput id="test1" text="{moneyCurrent}">

                using the {} will bind the value in test1 to whatever value moneyCurrent has. if its a label you are wanting you can do the same just change to <mx:Label... Both label and textInput have text property.

                So even if the value of moneyCurrent is being changed somewhere else, the textinput or label field will be automatically updated.
                Hope that resolves your problem
                • 5. Re: A Simple XMLListCollection Question
                  January_jsd Level 1
                  Thanks for all the responses, but I've solved the problem. Solved in a cowardly, yet strangely satisfying way - I stopped trying to import these few values from XML, wrote them out to a text file, and imported that instead. Job done :D

                  Although, this does lead me to another question - caching. How do you stop your application from reading cached data? It's like if I download my XML/text files, if they're changed on the server, the SWF reads the files cached on my computer/user's computer, ignoring the new data available (if you clear your browser's cache, this obviously forces the app to read the data from the server).

                  Is there a simple way of switching of caching, or ... see below:
                  • 6. Re: A Simple XMLListCollection Question
                    amthekkel Level 1
                    Good that you fond a workaround. Not sure that using the text based option will be the best bet overall as XML data is usually best for transporting back and forth. I would still suggest that you try and get it working from xml data. As you are new to flex it will be a good learning curve for the future and i am sure the benefits would be there if you can solve the problem with xml data