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

    XML attributes as a response

      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.:
      <response type="object" id="unknown">
      <generic response="Success" message="Authentication accepted"/>

      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:

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

      Thank you,

        • 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):

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
          <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 relativeTo="{panel1}" position="lastChild">
          <mx:Label x="10" y="62" text="Label"/>
          import mx.rpc.events.ResultEvent;
          var MyXML:XML;

          private function checkLogin(evt:ResultEvent):void
          MyXML = evt.result.channel.item;

          /* 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:HTTPService id="login_user" result="checkLogin(event)" showBusyCursor="true" method="GET" url="" useProxy="false">
          <mx:request xmlns="">

          <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();"/>

          • 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