今天学习XMLSocket,可是在拿到XML字符串数据后,不知该如何解析,JavaEye真是个好地方,终于找到了思路:
http://xiaoer-1982.iteye.com/blog/500333
首先由XML得到XMLList,然后构造出XMLListCollection,最后得到Array,最后构造ArrayCollection。
原始XML字符串:
<response>
<resp time="" no="" lat="" lng="" />
<resp ............ />
</response>
var response:XML = new XML(event.data);
var xmlListColl:XMLListCollection = new XMLListCollection();
xmlListColl.source = response.elements("resp");
var arr:Array = xmlListColl.toArray();
var arrColl:ArrayCollection = new ArrayCollection();
arrColl.source = arr;
for(var i:int = 0; i < arrColl.length; i++)
{
var resp:Object = arrColl.getItemAt(i);
result.text += resp.@time + ":" + resp.@no + "[" + resp.@lat + ", " + resp.@lng + "]\n";
txtiptName.text = resp.@no;
this.onChange(1);
}
将整个AS文件发上来:
/**
* ActionScript file for ArrayCarDemo.mxml
*/
import flash.events.DataEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.Socket;
import flash.net.XMLSocket;
import flash.system.Security;
import flash.xml.XMLDocument;
import mx.collections.ArrayCollection;
import mx.collections.XMLListCollection;
import mx.rpc.xml.SimpleXMLDecoder;
private var socket:Socket;
private var xmlSocket:XMLSocket;
private function onConnect():void
{
result.text += "==>开始连接......\n";
Security.loadPolicyFile("xmlsocket://GMapWeb:9999");
xmlSocket = new XMLSocket();
xmlSocket.connect("GMapWeb", 9999);
xmlSocket.addEventListener(Event.CONNECT, onConnected);
xmlSocket.addEventListener(Event.CLOSE,onClosed);
xmlSocket.addEventListener(DataEvent.DATA,onRecvData);
xmlSocket.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
xmlSocket.addEventListener(ProgressEvent.PROGRESS, onProgress);
xmlSocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecureError);
}
private function onIoError(evt:IOErrorEvent):void
{
result.text += "IO错误:" + evt.text + "\n";
}
private function onProgress(evt:ProgressEvent):void
{
result.text += "Progress:" + evt.target.toString() + "\n";
}
private function onSecureError(evt:SecurityErrorEvent):void
{
result.text += "安全错误:" + evt.text + "\n";
}
internal function onConnected(event:Event):void
{
// xmlSocket.send("<test/>\n");
if(xmlSocket.connected)
{
result.text += "已经成功连接到服务器\n";
}
else
{
result.text += "失败:连接到服务器\n";
}
}
internal function onClosed(event:Event):void
{
result.text += "已经断开服务器\n";
}
private function onRecvData(event:DataEvent):void
{
var response:XML = new XML(event.data);
var xmlListColl:XMLListCollection = new XMLListCollection();
xmlListColl.source = response.elements("resp");
var arr:Array = xmlListColl.toArray();
var arrColl:ArrayCollection = new ArrayCollection();
arrColl.source = arr;
for(var i:int = 0; i < arrColl.length; i++)
{
var resp:Object = arrColl.getItemAt(i);
result.text += resp.@time + ":" + resp.@no + "[" + resp.@lat + ", " + resp.@lng + "]\n";
txtiptName.text = resp.@no;
this.onChange(1);
}
result.text += "------------------------";
result.verticalScrollPosition = result.maxVerticalScrollPosition;
}
private function sendXml():void
{
//xmlSocket.send("<msg>" + message.text + "</msg>");
//result.text += message.text + "\n";
//message.text = "";
}