Skip to content

Instantly share code, notes, and snippets.

@sugamasao
Created October 28, 2010 02:59
Show Gist options
  • Save sugamasao/650527 to your computer and use it in GitHub Desktop.
Save sugamasao/650527 to your computer and use it in GitHub Desktop.
XML を JSのデータ形式に戻す
// 要 jQuery
var to_native = function(type, str) {
if(type === "integer") {
return str-0
} else if(type == "boolean") {
return !!str
} else if(type == "datetime") {
return new Date(Date.parse(str))
}
return str
}
var to_hash = function(element) {
var ret = {}
for each(var elem in element.children) {
if(elem.constructor == Element) {
var val = ""
var e = $(elem)
var type = e.attr("type").toString().toLowerCase();
if(type != "array") {
val = to_native(type, e.text())
} else {
val = [arguments.callee(elem)]
}
ret[elem.nodeName.toString()] = val
}
}
return ret
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment