UNPKG

node-x2js

Version:

x2js - XML to JSON and vice versa for JavaScript

2 lines (1 loc) 6.88 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.X2JS=t()}(this,function(){"use strict";return function(e){function t(e){var t=e.localName;return null==t&&(t=e.baseName),null!=t&&""!=t||(t=e.nodeName),t}function n(e){return e.prefix}function r(e){return"string"==typeof e?e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&apos;"):e}function i(e,t,n,r){for(var i=0;i<e.length;i++){var a=e[i];if("string"==typeof a){if(a==r)break}else if(a instanceof RegExp){if(a.test(r))break}else if("function"==typeof a&&a(t,n,r))break}return i!=e.length}function a(t,n,r){switch(e.arrayAccessForm){case"property":t[n]instanceof Array?t[n+"_asArray"]=t[n]:t[n+"_asArray"]=[t[n]]}!(t[n]instanceof Array)&&e.arrayAccessFormPaths.length>0&&i(e.arrayAccessFormPaths,t,n,r)&&(t[n]=[t[n]])}function s(e){var t=e.split(/[-T:+Z]/g),n=new Date(t[0],t[1]-1,t[2]),r=t[5].split(".");if(n.setHours(t[3],t[4],r[0]),r.length>1&&n.setMilliseconds(r[1]),t[6]&&t[7]){var i=60*t[6]+Number(t[7]);i=0+("-"==(/\d\d-\d\d:\d\d$/.test(e)?"-":"+")?-1*i:i),n.setMinutes(n.getMinutes()-i-n.getTimezoneOffset())}else-1!==e.indexOf("Z",e.length-1)&&(n=new Date(Date.UTC(n.getFullYear(),n.getMonth(),n.getDate(),n.getHours(),n.getMinutes(),n.getSeconds(),n.getMilliseconds())));return n}function o(t,n,r){if(e.datetimeAccessFormPaths.length>0){var a=r.split(".#")[0];return i(e.datetimeAccessFormPaths,t,n,a)?s(t):t}return t}function l(t,n,r,a){return!(n==E.ELEMENT_NODE&&e.xmlElementsFilter.length>0)||i(e.xmlElementsFilter,t,r,a)}function c(r,i){if(r.nodeType==E.DOCUMENT_NODE){for(var s={},u=r.childNodes,f=0;f<u.length;f++){var _=u.item(f);if(_.nodeType==E.ELEMENT_NODE){var d=t(_);s[d]=c(_,d)}}return s}if(r.nodeType==E.ELEMENT_NODE){var p={};p.__cnt=0;for(var m=r.childNodes,x="",g=0;g<m.length;g++){var h=m.item(g);if(x=t(h),h.nodeType!=E.COMMENT_NODE){var y=i+"."+x;l(p,h.nodeType,x,y)&&(p.__cnt++,null==p[x]?(p[x]=c(h,y),a(p,x,y)):(null!=p[x]&&(p[x]instanceof Array||(p[x]=[p[x]],a(p,x,y))),p[x][p[x].length]=c(h,y)))}}for(var v=0;v<r.attributes.length;v++){var O=r.attributes.item(v);p.__cnt++,p[e.attributePrefix+O.name]=O.value}var N=n(r);return null!=N&&""!=N&&(p.__cnt++,p.__prefix=N),null!=p["#text"]&&(p.__text=p["#text"],p.__text instanceof Array&&(p.__text=p.__text.join("\n")),e.stripWhitespaces&&(p.__text=p.__text.trim()),delete p["#text"],"property"==e.arrayAccessForm&&delete p["#text_asArray"],p.__text=o(p.__text,x,i+"."+x)),null!=p["#cdata-section"]&&(p.__cdata=p["#cdata-section"],delete p["#cdata-section"],"property"==e.arrayAccessForm&&delete p["#cdata-section_asArray"]),0==p.__cnt&&"text"==e.emptyNodeForm?p="":1==p.__cnt&&null!=p.__text?p=p.__text:1!=p.__cnt||null==p.__cdata||e.keepCData?p.__cnt>1&&null!=p.__text&&e.skipEmptyTextNodesForObj&&(e.stripWhitespaces&&""==p.__text||""==p.__text.trim())&&delete p.__text:p=p.__cdata,delete p.__cnt,!e.enableToStringFunc||null==p.__text&&null==p.__cdata||(p.toString=function(){return(null!=this.__text?this.__text:"")+(null!=this.__cdata?this.__cdata:"")}),p}if(r.nodeType==E.TEXT_NODE||r.nodeType==E.CDATA_SECTION_NODE)return r.nodeValue}function u(t,n,i,a){var s="<"+(null!=t&&null!=t.__prefix?t.__prefix+":":"")+n;if(null!=i)for(var o=0;o<i.length;o++){var l=i[o],c=t[l];e.escapeMode&&(c=r(c)),s+=" "+l.substr(e.attributePrefix.length)+"=",e.useDoubleQuotes?s+='"'+c+'"':s+="'"+c+"'"}return s+=a?"/>":">"}function f(e,t){return"</"+(null!=e.__prefix?e.__prefix+":":"")+t+">"}function _(e,t){return-1!==e.indexOf(t,e.length-t.length)}function d(t,n){return!!("property"==e.arrayAccessForm&&_(n.toString(),"_asArray")||0==n.toString().indexOf(e.attributePrefix)||0==n.toString().indexOf("__")||t[n]instanceof Function)}function p(e){var t=0;if(e instanceof Object)for(var n in e)d(e,n)||t++;return t}function m(t,n,r){return 0==e.jsonPropertiesFilter.length||""==r||i(e.jsonPropertiesFilter,t,n,r)}function x(t){var n=[];if(t instanceof Object)for(var r in t)-1==r.toString().indexOf("__")&&0==r.toString().indexOf(e.attributePrefix)&&n.push(r);return n}function g(t){var n="";return null!=t.__cdata&&(n+="<![CDATA["+t.__cdata+"]]>"),null!=t.__text&&(e.escapeMode?n+=r(t.__text):n+=t.__text),n}function h(t){var n="";return t instanceof Object?n+=g(t):null!=t&&(e.escapeMode?n+=r(t):n+=t),n}function y(e,t){return""===e?t:e+"."+t}function v(e,t,n,r){var i="";if(0==e.length)i+=u(e,t,n,!0);else for(var a=0;a<e.length;a++)i+=u(e[a],t,x(e[a]),!1),i+=O(e[a],y(r,t)),i+=f(e[a],t);return i}function O(e,t){var n="";if(p(e)>0)for(var r in e)if(!d(e,r)&&(""==t||m(e,r,y(t,r)))){var i=e[r],a=x(i);if(null==i||void 0==i)n+=u(i,r,a,!0);else if(i instanceof Object)if(i instanceof Array)n+=v(i,r,a,t);else if(i instanceof Date)n+=u(i,r,a,!1),n+=i.toISOString(),n+=f(i,r);else{var s=p(i);s>0||null!=i.__text||null!=i.__cdata?(n+=u(i,r,a,!1),n+=O(i,y(t,r)),n+=f(i,r)):n+=u(i,r,a,!0)}else n+=u(i,r,a,!1),n+=h(i),n+=f(i,r)}return n+=h(e)}e=e||{},function(){void 0===e.escapeMode&&(e.escapeMode=!0),e.attributePrefix=e.attributePrefix||"_",e.arrayAccessForm=e.arrayAccessForm||"none",e.emptyNodeForm=e.emptyNodeForm||"text",void 0===e.enableToStringFunc&&(e.enableToStringFunc=!0),e.arrayAccessFormPaths=e.arrayAccessFormPaths||[],void 0===e.skipEmptyTextNodesForObj&&(e.skipEmptyTextNodesForObj=!0),void 0===e.stripWhitespaces&&(e.stripWhitespaces=!0),e.datetimeAccessFormPaths=e.datetimeAccessFormPaths||[],void 0===e.useDoubleQuotes&&(e.useDoubleQuotes=!1),e.xmlElementsFilter=e.xmlElementsFilter||[],e.jsonPropertiesFilter=e.jsonPropertiesFilter||[],void 0===e.keepCData&&(e.keepCData=!1)}();var E={ELEMENT_NODE:1,TEXT_NODE:3,CDATA_SECTION_NODE:4,COMMENT_NODE:8,DOCUMENT_NODE:9};this.parseXmlString=function(e){var t=window.ActiveXObject||"ActiveXObject"in window;if(void 0===e)return null;var n=void 0;if(window.DOMParser){var r=new window.DOMParser,i=null;if(!t)try{i=r.parseFromString("INVALID","text/xml").getElementsByTagName("parsererror")[0].namespaceURI}catch(e){i=null}try{n=r.parseFromString(e,"text/xml"),null!=i&&n.getElementsByTagNameNS(i,"parsererror").length>0&&(n=null)}catch(e){n=null}}else 0==e.indexOf("<?")&&(e=e.substr(e.indexOf("?>")+2)),n=new ActiveXObject("Microsoft.XMLDOM"),n.async="false",n.loadXML(e);return n},this.asArray=function(e){return void 0===e||null==e?[]:e instanceof Array?e:[e]},this.toXmlDateTime=function(e){return e instanceof Date?e.toISOString():"number"==typeof e?new Date(e).toISOString():null},this.asDateTime=function(e){return"string"==typeof e?s(e):e},this.xml2json=function(e){return c(e)},this.xml_str2json=function(e){var t=this.parseXmlString(e);return null!=t?this.xml2json(t):null},this.json2xml_str=function(e){return O(e,"")},this.json2xml=function(e){var t=this.json2xml_str(e);return this.parseXmlString(t)},this.getVersion=function(){return"1.2.0"}}});