[JavaScript]rpcparam2hash函数[经典]
function xgetChildElement(doc)
{
for (var i = 0; i < doc.childNodes.length; i ++)
if (doc.childNodes[i].nodeName != "#text")
return doc.childNodes[i];
throw "No child exists";
return null;
}
function rpcparam2hash(doc)
{
var rpc;
if (doc.tagName == "methodResponse") {
rpc = rpcparam2hash(xgetChildElement(doc));
} else if (doc.tagName == "params" || doc.tagName == "fault") {
rpc = Array();
rpc[doc.tagName] = rpcparam2hash(xgetChildElement(doc));
} else if (doc.tagName == "value" || doc.tagName == "array"
|| doc.tagName == "param") {
rpc = rpcparam2hash(xgetChildElement(doc));
} else if (doc.tagName == "data") {
rpc = Array();
for (var i = 0; i < doc.childNodes.length; i++) {
if (doc.childNodes[i].nodeName != "#text") {
var myparam;
myparam = rpcparam2hash(doc.childNodes[i]);
rpc.push(myparam);
}
}
} else if (doc.tagName == "i4" || doc.tagName == "int") {
rpc = parseInt(doc.childNodes[0].nodeValue);
} else if (doc.tagName == "string") {
rpc = doc.childNodes[0].nodeValue;
} else if (doc.tagName == "boolean") {
rpc = new Boolean(parseInt(doc.childNodes[0].nodeValue));
} else if (doc.tagName == "double") {
rpc = parseFloat(doc.childNodes[0].nodeValue);
} else if (doc.tagName == "dateTime.iso8601") {
datestr = doc.childNodes[0].nodeValue;
rpc = new Date(parseInt(datestr.substr(0, 4), 10),
parseInt(datestr.substr(4, 2), 10),
parseInt(datestr.substr(6, 2), 10),
parseInt(datestr.substr(9, 2), 10),
parseInt(datestr.substr(12, 2), 10),
parseInt(datestr.substr(15, 2), 10));
} else if (doc.tagName == "struct") {
rpc = Array();
for (var x = 0; x < doc.childNodes.length; x++) {
var name;
var value;
var node = doc.childNodes[x];
if (node.nodeName == "member") {
for (var y = 0; y < node.childNodes.length; y++) {
var mynode = node.childNodes[y];
if (mynode.nodeName == "#text") continue;
if (mynode.tagName == "name") {
name = mynode.childNodes[0].nodeValue
} else if (mynode.tagName == "value") {
value = rpcparam2hash(mynode);
}
}
rpc[name] = value;
}
}
} else {
throw "Unknown/Unsupported tagname:" + doc.tagName;
}
return rpc;
}
{
for (var i = 0; i < doc.childNodes.length; i ++)
if (doc.childNodes[i].nodeName != "#text")
return doc.childNodes[i];
throw "No child exists";
return null;
}
function rpcparam2hash(doc)
{
var rpc;
if (doc.tagName == "methodResponse") {
rpc = rpcparam2hash(xgetChildElement(doc));
} else if (doc.tagName == "params" || doc.tagName == "fault") {
rpc = Array();
rpc[doc.tagName] = rpcparam2hash(xgetChildElement(doc));
} else if (doc.tagName == "value" || doc.tagName == "array"
|| doc.tagName == "param") {
rpc = rpcparam2hash(xgetChildElement(doc));
} else if (doc.tagName == "data") {
rpc = Array();
for (var i = 0; i < doc.childNodes.length; i++) {
if (doc.childNodes[i].nodeName != "#text") {
var myparam;
myparam = rpcparam2hash(doc.childNodes[i]);
rpc.push(myparam);
}
}
} else if (doc.tagName == "i4" || doc.tagName == "int") {
rpc = parseInt(doc.childNodes[0].nodeValue);
} else if (doc.tagName == "string") {
rpc = doc.childNodes[0].nodeValue;
} else if (doc.tagName == "boolean") {
rpc = new Boolean(parseInt(doc.childNodes[0].nodeValue));
} else if (doc.tagName == "double") {
rpc = parseFloat(doc.childNodes[0].nodeValue);
} else if (doc.tagName == "dateTime.iso8601") {
datestr = doc.childNodes[0].nodeValue;
rpc = new Date(parseInt(datestr.substr(0, 4), 10),
parseInt(datestr.substr(4, 2), 10),
parseInt(datestr.substr(6, 2), 10),
parseInt(datestr.substr(9, 2), 10),
parseInt(datestr.substr(12, 2), 10),
parseInt(datestr.substr(15, 2), 10));
} else if (doc.tagName == "struct") {
rpc = Array();
for (var x = 0; x < doc.childNodes.length; x++) {
var name;
var value;
var node = doc.childNodes[x];
if (node.nodeName == "member") {
for (var y = 0; y < node.childNodes.length; y++) {
var mynode = node.childNodes[y];
if (mynode.nodeName == "#text") continue;
if (mynode.tagName == "name") {
name = mynode.childNodes[0].nodeValue
} else if (mynode.tagName == "value") {
value = rpcparam2hash(mynode);
}
}
rpc[name] = value;
}
}
} else {
throw "Unknown/Unsupported tagname:" + doc.tagName;
}
return rpc;
}