外部XML(Web服务)的XML解析(Flash AS3)

时间:2012-12-27 07:09:02

标签: actionscript-3 xml-parsing

我的AS3代码:

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()

结果:

%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%图20是%20wrong%3C%2Fitem%3E%3C%2Ferror%3E%3C%2Fxml%3E%0A

怎么了?我需要的结果与

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

我很抱歉我的英语不好。

2 个答案:

答案 0 :(得分:1)

您可以尝试使用unescape(xmldata.toString())然后获得所需的输出。

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

函数escape(“args”)将参数转换为字符串,并以URL编码格式对其进行编码。

escape("escape unescape");

输出:escape%20unescape

函数unescape将所有十六进制序列转换为ASCII字符。例如,

unescape("escape%20unescape");

输出:escape unescape

答案 1 :(得分:0)

我确定这是因为您选择了URLLoaderDataFormat.VARIABLES类型,在将此传递给XML构造函数时,尝试使用URLLoaderDataFormat.TEXT等其他值,并使用try catch块将其包装,因为无效数据会抛出错误。