2 Replies Latest reply on May 7, 2008 1:04 PM by ntsiii

    XML attributes as a response

    jets_idaho
      My skills are rudimentary, I'm a new flex developer. I have done all of the tutorials and examples and I'm just struggling with using an XML attribute checking a response.

      I've written a simple flex 3 app to talk to Asterisk AJAM. I'm writing the login piece-- and i'm having trouble parsing this response from an http service (successful login) my problem comes in with the value not being as such <item>valuehere</item> it's part of the tag, an attribute i would suppose.:
      <ajax-response>
      <response type="object" id="unknown">
      <generic response="Success" message="Authentication accepted"/>
      </response>
      </ajax-response>

      What I have is a simple app with a http service and a login form. I have an event handler for the HTTP service, but I don't know how to parse for that generic response tag. Anyone know how to format an XML object for that type of response and throw a simple if statement looking for that attribute?

      Failure looks like this BTW:
      <ajax-response>

      <response type="object" id="unknown">
      <generic response="Error" message="Authentication failed"/>
      </response>
      </ajax-response>

      Thank you,

      Brian
        • 1. Re: XML attributes as a response
          jets_idaho Level 1
          I've came up with the following code trying to use XML attributes from the response above--- it doesn't work but here is what I have (right now the if statement is commented out.... i'm trying to have it do an alert which doesn't work either):

          ---source---
          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
          <mx:states>
          <mx:State name="Logged In">
          <mx:SetProperty target="{panel1}" name="width" value="95%"/>
          <mx:SetProperty target="{panel1}" name="height" value="95%"/>
          <mx:RemoveChild target="{password}"/>
          <mx:RemoveChild target="{username}"/>
          <mx:RemoveChild target="{label1}"/>
          <mx:RemoveChild target="{Submit}"/>
          <mx:RemoveChild target="{label2}"/>
          <mx:SetProperty target="{panel1}" name="title" value="Once Logged In"/>
          <mx:AddChild relativeTo="{panel1}" position="lastChild">
          <mx:Label x="10" y="10" text="You properly authentictaed!"/>
          </mx:AddChild>
          <mx:AddChild relativeTo="{panel1}" position="lastChild">
          <mx:Label x="10" y="62" text="Label"/>
          </mx:AddChild>
          </mx:State>
          </mx:states>
          <mx:Script>
          <![CDATA[
          import mx.rpc.events.ResultEvent;
          var MyXML:XML;


          private function checkLogin(evt:ResultEvent):void
          {
          MyXML = evt.result.channel.item;
          mx.controls.Alert.show(MyXML.@*);
          trace(MyXML.@*.toXMLString());

          /* if (MyXML.attribute(ajax-response.response.@generic) == "Success") {

          }
          if (MyXML.ajax-response.response.(@generic == "Success"))
          {
          currentState = "Logged In";
          }

          if (MyXML.ajax-response.response.(@generic == "Error"))
          {
          mx.controls.Alert.show('Invalid username/password');
          } */
          }

          ]]>

          </mx:Script>


          <mx:HTTPService id="login_user" result="checkLogin(event)" showBusyCursor="true" method="GET" url=" http://72.22.224.90:8088/asterisk/mxml" useProxy="false">
          <mx:request xmlns="">
          <action>login</action>
          <username>
          {username.text}
          </username>
          <password>
          {password.text}
          </password>
          </mx:request>
          </mx:HTTPService>

          <mx:Panel resizeEffect="Resize" width="250" height="200" layout="absolute" title="Login System" horizontalCenter="0" verticalCenter="-2" id="panel1">
          <mx:Label x="10" y="10" text="Username:" id="label1"/>
          <mx:TextInput x="10" y="36" id="username"/>
          <mx:Label x="10" y="66" text="Password:" id="label2"/>
          <mx:TextInput x="10" y="92" id="password" displayAsPassword="true"/>
          <mx:Button x="10" y="122" label="Submit" id="Submit" click="login_user.send();"/>
          </mx:Panel>

          </mx:Application>
          • 2. Re: XML attributes as a response
            ntsiii Level 3
            Yeah, this is because the default resultFormat if HTTPService, and the other RPC protocols as well, is "object" . this causes Flex to convert your XML into a nested object structure. This ia almost never desirable.

            Set resultFormat="e4x", and in your result handler, get the base result xml before applyijng any expressions, so you can be sure of what you have:
            ...
            var xmlResult:XML = XML(evt.result)
            trace(xmlResult.toXMLString()); //ALWAYS use toXMLString() to see xml
            MyXML = xmlResult.channel.item[0]; //because all e4x expressions return XMLList, not XML
            mx.controls.Alert.show(MyXML.@*);
            trace(MyXML.@*.toXMLString());

            Tracy