diff --git a/lib/xml2json.js b/lib/xml2json.js index bce932b..0a312a9 100644 --- a/lib/xml2json.js +++ b/lib/xml2json.js @@ -49,6 +49,18 @@ exports.parser = function(xmlcode,ignoretags,debug){ if(!ignoretags){ignoretags=""}; + // processing CDATA + var tmp_str = {}, + tmp_key = ''; + + var re = RegExp(//); + var f; + while (f = re.exec(xmlcode)) { + tmp_key = 'tmp_' + f.index; + tmp_str[tmp_key] = f[1]; + xmlcode=xmlcode.replace(//,tmp_key); + } + // processing CDATA end xmlcode=xmlcode.replace(/\s*\/>/g,'/>'); xmlcode=xmlcode.replace(/<\?[^>]*>/g,"").replace(/<\![^>]*>/g,""); if (!ignoretags.sort){ignoretags=ignoretags.split(",")}; @@ -60,10 +72,17 @@ exports.parser = function(xmlcode,ignoretags,debug){ x=x.replace(new RegExp("<"+ignoretags[i]+">","g"),"*$**"+ignoretags[i]+"**$*"); x=x.replace(new RegExp("","g"),"*$***"+ignoretags[i]+"**$*") }; - x=''+x+''; + x=''+x+''; this.xmlobject={}; var y=this.xml_to_object(x).jsontagwrapper; if(debug){y=this.show_json_structure(y,debug)}; + //restore CDATA + var jst = JSON.stringify(y); + for (var key in tmp_str) { + jst = jst.replace(key, tmp_str[key]); + } + y = JSON.parse(jst); + // restore end return y } @@ -86,7 +105,6 @@ exports.xml_to_object = function(xmlcode){ var preeval=""; var niva=y[i].split("<")[0]; var tagnamn=y[i].split("<")[1].split(">")[0]; - tagnamn=tagnamn.toLowerCase(); var rest=y[i].split(">")[1]; if(niva<=oldniva){ var tabort=oldniva-niva+1; @@ -210,4 +228,4 @@ if (!Array.prototype.pop){ this.length--; return response } -}; \ No newline at end of file +};