@musicbird/xml-parser
Version:
MusicBird - XML Parser
1 lines • 1.91 kB
JavaScript
;Object.defineProperty(exports,'__esModule',{value:true});var __assign=function(){return __assign=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var o in e=arguments[r])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},__assign.apply(this,arguments)};var DEFAULT_CONFIG={doctypeKey:"doctype",rootKey:"root",tagKey:"name",attrsKey:"attrs",contentKey:"value",childrenKey:"children",normalize:!0,lowerCaseTagsContent:!0,lowerCaseTagsNames:!1};var sax=require("sax");function xmlParser(e,n){void 0===e&&(e=""),void 0===n&&(n={});var t=__assign(__assign({},DEFAULT_CONFIG),n),o=!t.lowerCaseTagsNames,r=sax.parser(o,{trim:!0,normalize:t.normalize,lowercase:t.lowerCaseTagsNames}),a=[],s=null,i={},l={ondoctype:function(e){var n=t.doctypeKey,o=e.trim().match(/(\"[^\"]+\")|([^\s]+)/gi);i[n]=o;},onopentag:function(e){var n,o=e.name,r=e.attributes,l=t.tagKey,c=t.childrenKey,u=t.attrsKey,f=t.rootKey,g=((n={})[l]=o,n);this.tag.isSelfClosing&&(g.isSelfClosing=!0);Object.keys(r).length&&(g[u]=r);if(!s)return i[f||o]=s=g;null==s[c]&&(s[c]=[]);a.push(s),s[c].push(s=g);},onclosetag:function(e){s=a.pop();},ontext:function(e){if(!s)return;var n=t.lowerCaseTagsContent?e.trim().toLowerCase():e.trim();s[t.contentKey]=n||null;},oncdata:function(e){if(!s)return;var n=e.trim();n&&(s[t.contentKey]=n);},onclosecdata:function(){null==s[t.contentKey]&&(s[t.contentKey]=null);},onerror:function(e){console.error(e);}};return Object.assign(r,l),r.write(e).close(),i}var NodeModel=function(){function e(e){this.name=e.name,this.attrs=e.attrs,this.value=e.value,this.isSelfClosing=e.isSelfClosing,this.children=e.children;}return e.prototype.findNode=function(e){return this.children.find((function(r){return r.name==e}))},e.prototype.getChildValue=function(e){var r=this.findNode(e);if(r&&null!=r.value)return r.value},e}();exports.NodeModel=NodeModel;exports.parseXml=xmlParser;