XML Parsing (Flash AS3) from external XML (web service)

My AS3 code:

loginBtn.addEventListener("mouseDown", sendData)
function sendData(e:Event) {
    var path:String = "http://server1.digitalmulia.com/~testing/webservice/auth/login";
    var myData:URLRequest = new URLRequest(path)
    myData.method = URLRequestMethod.POST
    var variables:URLVariables = new URLVariables()
    variables.username = usernameField.text
    variables.password = passwdField.text
    myData.data = variables
    var loader:URLLoader = new URLLoader()
    loader.dataFormat = URLLoaderDataFormat.VARIABLES
    loader.addEventListener(Event.COMPLETE, dataOnLoad)
    loader.load(myData)
}
function dataOnLoad(e:Event){
    var xmldata = XML(e.target.data);
    var xxx = xmldata.toString();
    var myFormat:TextFormat = new TextFormat();
    myFormat.font = "Arial";
    myFormat.size = 12;
    myFormat.bold = true;

    var myText:TextField = new TextField();
    myText.defaultTextFormat = myFormat;
    myText.selectable = true;
    myText.border = true;
    myText.text = xxx;
    myText.x = 10;
    myText.y = 200;
    myText.width = 300;
    myText.height = 100;
    addChild(myText);

}
stop()

The result:

%3C%3Fxml%20version=%221%2E0%22%3F%3E%0A%3Cxml%3E%3Cdatas%2F%3E%3Cinformation%3E%3Cpasswd%3E5f4dcc3b5aa765d61d8327deb882cf99%3C%2Fpasswd%3E%3C%2Finformation%3E%3Cerror%3E%3Citem%3EUsername%20not%20found%20or%20password%20is%20wrong%3C%2Fitem%3E%3C%2Ferror%3E%3C%2Fxml%3E%0A

What's wrong? The result which I need as similar as

<xml>
    <datas/>
    <information>
        <passwd>5f4dcc3b5aa765d61d8327deb882cf99</passwd>
    </information>
    <error>
        <item>Username is null</item>
        <item>Password is null</item>
    </error>
</xml>

I'm sorry for my bad english.

Upvotes: 0

Views: 764

Answers (2)

I'm sure it is becuase you have chosen URLLoaderDataFormat.VARIABLES type, try other values like URLLoaderDataFormat.TEXT also when passing this to the XML constructor wrap it with try catch block as invalid data will throw errors.

Upvotes: 0

Raja Jaganathan
Raja Jaganathan

Reputation: 36197

You can try with unescape(xmldata.toString()) then you will get desired output.

        function dataOnLoad(e:Event)
        {
            var xmldata:XML = XML(e.target.data);
            var xmlStr:String = unescape(xmldata.toString());
            xmldata = XML(xmlStr);          
        }

The function escape("args") converts the argument to a string and encodes it in a URL-encoded format.

escape("escape unescape");

output: escape%20unescape

The function unescape converts all hexadecimal sequences to ASCII characters. For example,

unescape("escape%20unescape");

output : escape unescape

Upvotes: 1

Related Questions