Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions lib/xml2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@

exports.parser = function(xmlcode,ignoretags,debug){
if(!ignoretags){ignoretags=""};
// processing CDATA
var tmp_str = {},
tmp_key = '';

var re = RegExp(/<!\[CDATA\[(.*?)\]\]>/);
var f;
while (f = re.exec(xmlcode)) {
tmp_key = 'tmp_' + f.index;
tmp_str[tmp_key] = f[1];
xmlcode=xmlcode.replace(/<!\[CDATA\[(.*?)\]\]>/,tmp_key);
}
// processing CDATA end
xmlcode=xmlcode.replace(/\s*\/>/g,'/>');
xmlcode=xmlcode.replace(/<\?[^>]*>/g,"").replace(/<\![^>]*>/g,"");
if (!ignoretags.sort){ignoretags=ignoretags.split(",")};
Expand All @@ -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("</"+ignoretags[i]+">","g"),"*$***"+ignoretags[i]+"**$*")
};
x='<JSONTAGWRAPPER>'+x+'</JSONTAGWRAPPER>';
x='<jsontagwrapper>'+x+'</jsontagwrapper>';
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
}

Expand All @@ -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;
Expand Down Expand Up @@ -210,4 +228,4 @@ if (!Array.prototype.pop){
this.length--;
return response
}
};
};