3 Replies Latest reply on Oct 23, 2006 8:18 AM by aBill

    XML Question

    ForsakenBlade
      I have a XML file with this information

      <Accountinfo>
      <username>Test</username>
      <password>test1</password>
      </AccountInfo>

      would it be possible for me to assign username or password to a Flex Variable and then display it in a label or how would i go about this?
        • 1. Re: XML Question
          aBill Level 1
          Hi

          I think HTTPService is your answer. It is better to look into documentation for details. http://download.macromedia.com/pub/documentation/en/flex/2/flex2_gettingstarted.pdf page 112
          It isn't long. Hope it help!

          William Chan
          • 2. Re: XML Question
            ForsakenBlade Level 1
            There are 2 problems with my script that I know of

            Problem #1:

            XML: (PHP FILLS THE $ variables)
            <Character>
            <CharacterInfo>
            <charID>$charID</charID>
            <charName>$charName</charName>
            <charGuild>$charGuild</charGuild>
            <charAge>$charAge</charAge>
            <charRace>$charRace</charRace>
            <charGender>$charGender</charGender>
            <charImage>$charImage</charImage>

            <charLocX>$locX</charLocX>
            <charLocY>$locY</charLocY>
            <charLocF>$locF</charLocF>
            </CharacterInfo>
            </Character>

            if I have CharacterInfo just have it show once it crashes so I have to have CharacterInfo repeat twice for some reason otherwise it gives an error.

            problem #2: I tried following the PDF, it tells you how to use variables for panels / datagrids but not how to create a variable in actionscript so then i could create an if statement and when the variable exists show a different .mxml file. I just need help with the converting the xml to a actionscript / flex variable.

            Here is the Code:

            login.mxml
            <mx:HTTPService id="sendLogin" url=" http://www.chimeraonlinerpg.com/CO/XML/login.php" result="loginHandler(event)" useProxy="false" method="POST">
            <mx:request xmlns="">
            <account>{accountInput.text}</account>
            <password>{passwordInput.text}</password>
            </mx:request>
            </mx:HTTPService>

            <mx:StringValidator id="accountValidator" source="{accountInput}" property="text" maxLength="15" minLength="5" triggerEvent="help" tooLongError="Account name may not contain more then 15 alphanumeric characters." tooShortError="Account name may not contain less then 5 alphanumeric characters."/>
            <mx:StringValidator id="passwordValidator" source="{passwordInput}" property="text" maxLength="15" minLength="5" triggerEvent="" tooLongError="Password may not contain more then 15 alphanumeric characters." tooShortError="Password may not contain less then 5 alphanumeric characters."/>

            <mx:DataGrid dataProvider="{loginData}"/>

            <mx:Form width="456" height="245">

            <mx:Panel width="100%" height="200" layout="absolute" title="Account Login">

            <mx:Label text="Password:" x="19" y="60"/>

            <mx:FormItem required="true" x="115" y="34" width="174">
            <mx:TextInput id="accountInput"/>
            </mx:FormItem>

            <mx:Label text="Account Name:" x="19" y="34"/>

            <mx:FormItem required="true" x="115" y="64">
            <mx:TextInput id="passwordInput" displayAsPassword="true"/>
            </mx:FormItem>

            <mx:Button id="buttonLogin" label="Login" click="sendLogin.send()" x="174" y="94"/>
            </mx:Panel>

            </mx:Form>


            functions.as

            import mx.collections.ArrayCollection;
            import mx.rpc.events.ResultEvent;
            import flash.xml.XMLNode;


            [Bindable]
            private var tileData:ArrayCollection;

            [Bindable]
            private var loginData:ArrayCollection;

            private function resultHandler(event:ResultEvent):void {
            tileData = event.result.tiles.tile;

            }

            private function loginHandler(event:ResultEvent):void {
            loginData = event.result.Character.CharacterInfo;

            }

            • 3. XML Question
              aBill Level 1
              Hi,

              If your xml only has one child, you can just use Object instead of ArrayCollection. Inside a xml, only same name tags are convert to array, otherwise, it is an object. If it is an object, you cannot bind it to DataGrid. You have to create an Array (or ArrayCollection) to make it work. For example:
              <DataGrid dataProvider="{[sendLogin.lastResult]}" /> Using [ ] is a literal array
              In actionscript, you can loginData = new Array(); loginData.push(event.result);
              or
              loginData = new ArrayCollection(); loginData.addItem(event.result);
              or
              loginData = [ event.result] ; or loginData = new ArrayCollection([event.result]);

              ArrayCollection is a better choice in databinding, since it has propertyChangeEvent in adding or removing item. The binded target can do update when event fired.

              If you want the result is bindable, you can put makeObjectsBindable="true" in your HTTPService tag. Without specify the resultFormat in your HTTPService, it is deserialized as object. You don't have to convert if by your own!