-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathxml.min.js
3 lines (3 loc) · 1.5 KB
/
xml.min.js
1
2
3
(function(h,j){"".trim||(String.prototype.trim=function(){return this.replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g,"")});var k,l;k={isUnsafe:!1,isXML:function(a){return"object"===typeof a&&a.nodeType!==j},getRoot:function(a){return 9===a.nodeType?a.documentElement:11===a.nodeType?a.firstChild:a},convert:function(a){var c={};if("string"===typeof a){var b=null,d=null,e=!0;try{b="DOMParser"in h?new DOMParser:new ActiveXObject("MSXML2.DOMDocument"),b.async=!1}catch(f){throw Error("XML Parser could not be instantiated");
}"parseFromString"in b?(d=b.parseFromString(a,"text/xml"),e="parsererror"!==d.documentElement.tagName):d=(e=b.loadXML(a))?b:!1;if(!e)throw Error("Error parsing XML string");a=d}else a=this.isXML(a)?a:j;if(!a)throw Error("Unable to parse XML");if(3===a.nodeType||4===a.nodeType)return a.nodeValue;a=this.getRoot(a);c[a.nodeName]={};this.process(a,c[a.nodeName]);return c},process:function(a,c){var b,d,e,f,g;if(a.hasChildNodes()){f=a.childNodes.length;for(e=0;e<f;e++)switch(b=a.childNodes[e],b.nodeType){case 3:c.Text=
c.Text?c.Text+b.nodeValue.trim():b.nodeValue.trim();break;case 4:b=b[b.text?"text":"nodeValue"];c.Text=c.Text?c.Text+b:b;break;case 1:d=b.nodeName,g={},d in c?c[d].length?(this.process(b,g),c[d].push(g)):(this.process(b,g),c[d]=[c[d],g]):(this.process(b,g),c[d]=g)}}if(a.attributes.length)for(f=a.attributes.length-1;0<=f;f--)b=a.attributes[f],e=b.name.trim(),b=b.value,c[(this.isUnsafe?"":"@")+e]=b}};l={xmlToJSON:function(a,c){k.isUnsafe=c!==j?c:!1;return k.convert(a)}};h.xml=h.xml||l})(window);