?? unmarshaller.as
字號:
?class XMLRPC.Unmarshaller {
public var res:XMLRPC.Result;
public function XMLToAS(xml:XML) {
this.res = new XMLRPC.Result();
this.res.Xml = xml;
//start inside the methodResponse tag
var root = xml.childNodes[0];
//now check if there was a fault
var n = root.childNodes[0];
if(n.nodeName == "fault") {
//build a fault structure starting at the value/struct tag inside the fault
var f = handleFault(n.childNodes[0].childNodes[0]);
this.res.Fault = f;
} else {
//build an array to hold all the return values
//start at the /methodResponse/params/param/value child node
var r = handleResult(n.childNodes[0].childNodes[0].childNodes[0]);
this.res.Data = r;
}
return this.res;
}
private function handleFault(struct:XML) {
var name:String;
var c:Number;
var d:String;
//the fault should have a specific structure, supposedly with only fault code and description
//but the elements of the struct are unordered so we'll have to cycle through it to parse it
//so iterate over the member nodes
for(var i:Number = 0; i < struct.childNodes.length; i++) {
//look at the name of this struct member
name = struct.childNodes[i].childNodes[0].childNodes[0].nodeValue;
//store it as the code or the description
//ignore it otherwise
if(name == "faultCode") {
c = Number(struct.childNodes[i].childNodes[1].childNodes[0].childNodes[0].nodeValue);
} else if(name == "faultString") {
d = struct.childNodes[i].childNodes[1].childNodes[0].childNodes[0].nodeValue;
}
}
return {code: c, description: d};
}
private function handleResult(node:XML) {
var d;
//switch based on the type of data returned
switch(node.nodeName) {
case "int":
case "i4":
d = Number(node.childNodes[0].nodeValue);
break;
case "string":
d = String(node.childNodes[0].nodeValue);
break;
case "double":
d = Number(node.childNodes[0].nodeValue);
break;
case "boolean":
d = Boolean(Number(node.childNodes[0].nodeValue));
break;
case "base64":
d = String(node.childNodes[0].nodeValue);
break;
case "dateTime.iso8601":
d = addDate(node.childNodes[0].nodeValue);
break;
case "array":
//start parsing the array at the data tag
d = addArray(node.childNodes[0]);
break;
case "struct":
//start parsingthe struct at the struct tag
d = addStruct(node);
break;
}
return d;
}
private function addArray(node:XMLNode) {
var i:Number;
var a:Array = new Array();
//parse the XML for an array, go through the value nodes
for(i=0; i < node.childNodes.length; i++) {
//get the child of the value node and handle it
a.push(handleResult(node.childNodes[i].childNodes[0]));
}
return a;
}
private function addStruct(node:XMLNode) {
var i:Number;
var j:Number;
var o:Object = new Object();
var v;
var n:String;
//parse the XML for a struct, go through the member nodes
for(i=0; i < node.childNodes.length; i++) {
var m = node.childNodes[i];
for(j=0; j < m.childNodes.length; j++) {
if(m.childNodes[j].nodeName == "name") {
//get the name child of the struct
n = m.childNodes[j].childNodes[0].nodeValue;
} else {
//get the value child of the struct
v = handleResult(m.childNodes[j].childNodes[0]);
}
}
//add the name/value pair to the anonymous object
o[n] = v;
}
return o;
}
private function addDate(d:String) {
//build a new date object
var obj = new Date();
//parse the date string
obj.setUTCFullYear(Number(d.substr(0, 4)));
obj.setUTCMonth(Number(d.substr(4, 2))-1);
obj.setUTCDate(Number(d.substr(6, 2)));
obj.setUTCHours(Number(d.substr(9, 2)));
obj.setUTCMinutes(Number(d.substr(12, 2)));
obj.setUTCSeconds(Number(d.substr(15, 2)));
return obj;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -