1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
| var utils = { json2xml:function(){}, xml2json:function(){} }; (function () { var XML = function () {}; XML.ObjTree = function () { return this }; XML.ObjTree.VERSION = "0.23"; XML.ObjTree.prototype.xmlDecl = '<?xml version="1.0" encoding="UTF-8" ?>\n'; XML.ObjTree.prototype.attr_prefix = ''; XML.ObjTree.prototype.parseXML = function (xml) { var root; if (window.DOMParser) { var xmldom = new DOMParser(); var dom = xmldom.parseFromString(xml, "application/xml"); if (!dom) return !1; root = dom.documentElement } else if (window.ActiveXObject) { xmldom = new ActiveXObject('Microsoft.XMLDOM'); xmldom.async = false; xmldom.loadXML(xml); root = xmldom.documentElement } if (!root) return !1; return this.parseDOM(root) }; XML.ObjTree.prototype.parseElement = function (elem) { if (elem.nodeType == 7) { return !1 } if (elem.nodeType == 3 || elem.nodeType == 4) { var bool = elem.nodeValue.match(/[^\x00-\x20]/); if (bool == null) return !1; return elem.nodeValue } var retval; var cnt = {}; if (elem.attributes && elem.attributes.length) { retval = {}; for (var i = 0; i < elem.attributes.length; i++) { var key = elem.attributes[i].nodeName; if (typeof (key) != "string") continue; var val = elem.attributes[i].nodeValue; if (!val) continue; key = this.attr_prefix + key; if (typeof (cnt[key]) == "undefined") cnt[key] = 0; cnt[key]++; this.addNode(retval, key, cnt[key], val) } } if (elem.childNodes && elem.childNodes.length) { var textonly = true; if (retval) textonly = false; for (var i = 0; i < elem.childNodes.length && textonly; i++) { var ntype = elem.childNodes[i].nodeType; if (ntype == 3 || ntype == 4) continue; textonly = false } if (textonly) { if (!retval) retval = ""; for (var i = 0; i < elem.childNodes.length; i++) { retval += elem.childNodes[i].nodeValue } } else { if (!retval) retval = {}; for (var i = 0; i < elem.childNodes.length; i++) { var key = elem.childNodes[i].nodeName; if (typeof (key) != "string") continue; var val = this.parseElement(elem.childNodes[i]); if (val !== "" && !val) continue; if (typeof (cnt[key]) == "undefined") cnt[key] = 0; cnt[key]++; this.addNode(retval, key, cnt[key], val) } } } else { retval = ''; } return retval }; XML.ObjTree.prototype.parseDOM = function (root) { if (!root) { return !1 }; this.__force_array = {}; if (this.force_array) { for (var i = 0; i < this.force_array.length; i++) { this.__force_array[this.force_array[i]] = 1 } } var json = this.parseElement(root); if (this.__force_array[root.nodeName]) { json = [json] } if (root.nodeType != 11) { var tmp = {}; tmp[root.nodeName] = json; json = tmp } return json }; XML.ObjTree.prototype.addNode = function (hash, key, cnts, val) { if (this.__force_array[key]) { if (cnts == 1) hash[key] = []; hash[key][hash[key].length] = val } else if (cnts == 1) { hash[key] = val } else if (cnts == 2) { hash[key] = [hash[key], val] } else { hash[key][hash[key].length] = val } }; XML.ObjTree.prototype.writeXML = function (tree) { var xml = this.hash_to_xml(null, tree); return this.xmlDecl + xml }; XML.ObjTree.prototype.hash_to_xml = function (name, tree) { var elem = []; var attr = []; for (var key in tree) { if (!tree.hasOwnProperty(key)) continue; var val = tree[key]; if (key.charAt(0) != this.attr_prefix) { if (typeof (val) == "undefined" || val == null) { elem[elem.length] = "<" + key + " />" } else if (typeof (val) == "object" && val.constructor.name == 'Array') { elem[elem.length] = this.array_to_xml(key, val) } else if (typeof (val) == "object") { elem[elem.length] = this.hash_to_xml(key, val) } else { elem[elem.length] = this.scalar_to_xml(key, val) } } else { attr[attr.length] = " " + (key.substring(1)) + '="' + (this.xml_escape(val)) + '"' } } var jattr = attr.join(""); var jelem = elem.join(""); if (typeof (name) == "undefined" || name == null) {} else if (elem.length > 0) { if (jelem.match(/\n/)) { jelem = "<" + name + jattr + ">\n" + jelem + "</" + name + ">\n" } else { jelem = "<" + name + jattr + ">" + jelem + "</" + name + ">\n" } } else { jelem = "<" + name + jattr + " />\n" } return jelem }; XML.ObjTree.prototype.array_to_xml = function (name, array) { var out = []; for (var i = 0; i < array.length; i++) { var val = array[i]; if (typeof (val) == "undefined" || val == null) { out[out.length] = "<" + name + " />" } else if (typeof (val) == "object" && val.constructor == Array) { out[out.length] = this.array_to_xml(name, val) } else if (typeof (val) == "object") { out[out.length] = this.hash_to_xml(name, val) } else { out[out.length] = this.scalar_to_xml(name, val) } } return out.join("") }; XML.ObjTree.prototype.scalar_to_xml = function (name, text) { if (name == "#text") { return this.xml_escape(text) } else { return "<" + name + ">" + this.xml_escape(text) + "</" + name + ">\n" } }; XML.ObjTree.prototype.xml_escape = function (text) { return (text + '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"') }; var JKL = function () {}; JKL.Dumper = function () { return this }; JKL.Dumper.prototype.dump = function (data, offset) { if (typeof (offset) == "undefined") offset = ""; var nextoff = offset + " "; switch (typeof (data)) { case "string": return '"' + this.escapeString(data) + '"'; break; case "number": return data; break; case "boolean": return data ? "true" : "false"; break; case "undefined": return "null"; break; case "object": if (data == null) { return "null" } else if (data.constructor == Array) { var array = []; for (var i = 0; i < data.length; i++) { array[i] = this.dump(data[i], nextoff) } return "[\n" + nextoff + array.join(",\n" + nextoff) + "\n" + offset + "]" } else { var array = []; for (var key in data) { var val = this.dump(data[key], nextoff); key = '"' + this.escapeString(key) + '"'; array[array.length] = key + ": " + val } if (array.length == 1 && !array[0].match(/[\n\{\[]/)) { return "{ " + array[0] + " }" } return "{\n" + nextoff + array.join(",\n" + nextoff) + "\n" + offset + "}" } break; default: return data; break } }; JKL.Dumper.prototype.escapeString = function (str) { return str.replace(/\\/g, "\\\\").replace(/\"/g, "\\\"") }; var formatXml = function (xml) { var reg = /(>)(<)(\/*)/g; var wsexp = / *(.*) +\n/g; var contexp = /(<.+>)(.+\n)/g; xml = xml.replace(reg, '$1\n$2$3').replace(wsexp, '$1\n').replace(contexp, '$1\n$2'); var pad = 0; var formatted = ''; var lines = xml.split('\n'); var indent = 0; var lastType = 'other'; var transitions = { 'single->single': 0, 'single->closing': -1, 'single->opening': 0, 'single->other': 0, 'closing->single': 0, 'closing->closing': -1, 'closing->opening': 0, 'closing->other': 0, 'opening->single': 1, 'opening->closing': 0, 'opening->opening': 1, 'opening->other': 1, 'other->single': 0, 'other->closing': -1, 'other->opening': 0, 'other->other': 0 }; for (var i = 0; i < lines.length; i++) { var ln = lines[i]; var single = Boolean(ln.match(/<.+\/>/)); var closing = Boolean(ln.match(/<\/.+>/)); var opening = Boolean(ln.match(/<[^!].*>/)); var type = single ? 'single' : closing ? 'closing' : opening ? 'opening' : 'other'; var fromTo = lastType + '->' + type; lastType = type; var padding = ''; indent += transitions[fromTo]; for (var j = 0; j < indent; j++) { padding += '\t' } if (fromTo == 'opening->closing') formatted = formatted.substr(0, formatted.length - 1) + ln + '\n'; else formatted += padding + ln + '\n' } return formatted };
var xotree = new XML.ObjTree(); var xml2json = function (xmlStr) { return xotree.parseXML(xmlStr); }; var json2xml = function (json) { return xotree.writeXML(json); }; utils.json2xml = json2xml; utils.xml2json = xml2json; })();
|