From f1b897ebde1751cf79c2d9dd81349106197f57ac Mon Sep 17 00:00:00 2001 From: Kyle Wong Date: Mon, 10 Aug 2015 17:20:01 +0800 Subject: [PATCH 1/3] implement CDATA section in xml to json --- lib/xml2json.js | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/xml2json.js b/lib/xml2json.js index bce932b..fec349a 100644 --- a/lib/xml2json.js +++ b/lib/xml2json.js @@ -49,6 +49,27 @@ exports.parser = function(xmlcode,ignoretags,debug){ if(!ignoretags){ignoretags=""}; + // processing CDATA + var tmp_str = {}, + h = -1, + t = -1, + tmp_key = ''; + + h = xmlcode.search(/ -1) { + + t = xmlcode.search(/\s*]]>/); + if (t < 0) { + break; + } + tmp_key = 'tmp_' + h; + tmp_str[tmp_key] = xmlcode.substr(h+9, t-h-9); + xmlcode=xmlcode.replace(/\/,''); + xmlcode=xmlcode.replace(tmp_str[tmp_key],tmp_key); + h = xmlcode.search(//g,'/>'); xmlcode=xmlcode.replace(/<\?[^>]*>/g,"").replace(/<\![^>]*>/g,""); if (!ignoretags.sort){ignoretags=ignoretags.split(",")}; @@ -64,6 +85,15 @@ exports.parser = function(xmlcode,ignoretags,debug){ 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 } @@ -210,4 +240,4 @@ if (!Array.prototype.pop){ this.length--; return response } -}; \ No newline at end of file +}; From 5b8708622a61236a3c4e73fb366da87a42302dd5 Mon Sep 17 00:00:00 2001 From: Kyle Wong Date: Mon, 10 Aug 2015 18:01:26 +0800 Subject: [PATCH 2/3] use regexp processing strings --- lib/xml2json.js | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/lib/xml2json.js b/lib/xml2json.js index fec349a..b573039 100644 --- a/lib/xml2json.js +++ b/lib/xml2json.js @@ -51,23 +51,14 @@ exports.parser = function(xmlcode,ignoretags,debug){ if(!ignoretags){ignoretags=""}; // processing CDATA var tmp_str = {}, - h = -1, - t = -1, tmp_key = ''; - h = xmlcode.search(/ -1) { - - t = xmlcode.search(/\s*]]>/); - if (t < 0) { - break; - } - tmp_key = 'tmp_' + h; - tmp_str[tmp_key] = xmlcode.substr(h+9, t-h-9); - xmlcode=xmlcode.replace(/\/,''); - xmlcode=xmlcode.replace(tmp_str[tmp_key],tmp_key); - h = xmlcode.search(//); + 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,'/>'); @@ -85,7 +76,6 @@ exports.parser = function(xmlcode,ignoretags,debug){ 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) { @@ -93,7 +83,6 @@ exports.parser = function(xmlcode,ignoretags,debug){ } y = JSON.parse(jst); // restore end - return y } From bfc8cbe6e4e19733c7c397138d9109b851b04a4c Mon Sep 17 00:00:00 2001 From: Kyle Wong Date: Wed, 26 Aug 2015 09:37:27 +0800 Subject: [PATCH 3/3] no need to swich tagname upper/lower case, keep the tagname as-is --- lib/xml2json.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/xml2json.js b/lib/xml2json.js index b573039..0a312a9 100644 --- a/lib/xml2json.js +++ b/lib/xml2json.js @@ -72,7 +72,7 @@ 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)}; @@ -105,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;