Created
November 29, 2011 23:54
-
-
Save therealklanni/1407231 to your computer and use it in GitHub Desktop.
A simple XML to JSON converter. Originally based on http://goo.gl/xfG0l and simplified.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xml2json = function(xml, extended) { | |
var root, out, | |
jsVar = function(s) { | |
return String(s || "").replace(/-/g,"_"); | |
}, | |
isNum = function(s) { | |
return (typeof s === "number") || String((s && typeof s === "string") ? s : "").test(/^((-)?([0-9]*)((\.{0,1})([0-9]+))?$)/); | |
}, | |
myArr = function(o) { | |
if (!o.length) { | |
o = [ o ]; | |
o.length = o.length; | |
} | |
return o; | |
}; | |
if (!xml) { | |
return; | |
} | |
function parseXML(node, simple) { | |
var txt = "", obj = null, att = null, | |
nt = node.nodeType, nn = jsVar(node.localName || node.nodeName), | |
nv = node.text || node.nodeValue || ""; | |
if (!node) { | |
return; | |
} | |
if (node.childNodes) { | |
if (node.childNodes.length > 0) { | |
$.each(node.childNodes, function(n,cn) { | |
var cnt = cn.nodeType, cnn = jsVar(cn.localName || cn.nodeName), | |
cnv = cn.text || cn.nodeValue || ""; | |
if (cnt === 8) { | |
return; | |
} else if (cnt === 3 || cnt === 4 || !cnn) { | |
if (cnv.match(/^\s+$/)) { | |
return; | |
} | |
txt += cnv.replace(/^\s+/,"").replace(/\s+$/,""); | |
} else { | |
obj = obj || {}; | |
if (obj[cnn]) { | |
if (!obj[cnn].length) { | |
obj[cnn] = myArr(obj[cnn]); | |
} | |
obj[cnn][obj[cnn].length] = parseXML(cn, true); | |
obj[cnn].length = obj[cnn].length; | |
} else { | |
obj[cnn] = parseXML(cn); | |
} | |
} | |
}); | |
} | |
} | |
if (node.attributes) { | |
if (node.attributes.length > 0) { | |
att = {}; obj = obj || {}; | |
$.each(node.attributes, function(a,at) { | |
var atn = jsVar(at.name), atv = at.value; | |
att[atn] = atv; | |
if (obj[atn]) { | |
if (!obj[atn].length) obj[atn] = myArr(obj[atn]); | |
obj[atn][ obj[atn].length ] = atv; | |
obj[atn].length = obj[atn].length; | |
} else { | |
obj[atn] = atv; | |
} | |
}); | |
} | |
} | |
if (obj) { | |
obj = $.extend( (txt!='' ? new String(txt) : {}), obj || {}); | |
txt = (obj.text) ? (typeof(obj.text) === "object" ? obj.text : [obj.text || ""]).concat([txt]) : txt; | |
if (txt) obj.text = txt; | |
txt = ""; | |
} | |
var out = obj || txt; | |
if (extended) { | |
if (txt) out = {}; | |
txt = out.text || txt || ""; | |
if (txt) out.text = txt; | |
if (!simple) out = myArr(out); | |
} | |
return out; | |
} | |
// Convert plain text to xml | |
if (typeof xml === "string") { | |
// Remove the XML namespaces | |
xml = this.string2xml(xml.replace(/\w+:/ig, "")); | |
} | |
// Quick fail if not xml (or if this is a node) | |
if (!xml.nodeType) { | |
return; | |
} | |
if (xml.nodeType === 3 || xml.nodeType === 4) { | |
return xml.nodeValue; | |
} | |
// Find xml root node | |
root = (xml.nodeType === 9) ? xml.documentElement : xml; | |
// Convert XML to JSON | |
out = parseXML(root, true); | |
// Clean-up | |
xml = null, root = null; | |
delete xml, root; | |
// Send output | |
return out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment