42
|
1 |
/*
|
|
2 |
xml2json v 1.1
|
|
3 |
copyright 2005-2007 Thomas Frank
|
|
4 |
|
|
5 |
This program is free software under the terms of the
|
|
6 |
GNU General Public License version 2 as published by the Free
|
|
7 |
Software Foundation. It is distributed without any warranty.
|
|
8 |
*/
|
|
9 |
|
|
10 |
var xml2json={
|
|
11 |
parser:function(xmlcode,ignoretags,debug){
|
|
12 |
if(!ignoretags){ignoretags=""};
|
|
13 |
xmlcode=xmlcode.replace(/\s*\/>/g,'/>');
|
|
14 |
xmlcode=xmlcode.replace(/<\?[^>]*>/g,"").replace(/<\![^>]*>/g,"");
|
|
15 |
if (!ignoretags.sort){ignoretags=ignoretags.split(",")};
|
|
16 |
var x=this.no_fast_endings(xmlcode);
|
|
17 |
x=this.attris_to_tags(x);
|
|
18 |
x=escape(x);
|
|
19 |
x=x.split("%3C").join("<").split("%3E").join(">").split("%3D").join("=").split("%22").join("\"");
|
|
20 |
for (var i=0;i<ignoretags.length;i++){
|
|
21 |
x=x.replace(new RegExp("<"+ignoretags[i]+">","g"),"*$**"+ignoretags[i]+"**$*");
|
|
22 |
x=x.replace(new RegExp("</"+ignoretags[i]+">","g"),"*$***"+ignoretags[i]+"**$*")
|
|
23 |
};
|
|
24 |
x='<JSONTAGWRAPPER>'+x+'</JSONTAGWRAPPER>';
|
|
25 |
this.xmlobject={};
|
|
26 |
var y=this.xml_to_object(x).jsontagwrapper;
|
|
27 |
if(debug){y=this.show_json_structure(y,debug)};
|
|
28 |
return y
|
|
29 |
},
|
|
30 |
xml_to_object:function(xmlcode){
|
|
31 |
var x=xmlcode.replace(/<\//g,"§");
|
|
32 |
x=x.split("<");
|
|
33 |
var y=[];
|
|
34 |
var level=0;
|
|
35 |
var opentags=[];
|
|
36 |
for (var i=1;i<x.length;i++){
|
|
37 |
var tagname=x[i].split(">")[0];
|
|
38 |
opentags.push(tagname);
|
|
39 |
level++
|
|
40 |
y.push(level+"<"+x[i].split("§")[0]);
|
|
41 |
while(x[i].indexOf("§"+opentags[opentags.length-1]+">")>=0){level--;opentags.pop()}
|
|
42 |
};
|
|
43 |
var oldniva=-1;
|
|
44 |
var objname="this.xmlobject";
|
|
45 |
for (var i=0;i<y.length;i++){
|
|
46 |
var preeval="";
|
|
47 |
var niva=y[i].split("<")[0];
|
|
48 |
var tagnamn=y[i].split("<")[1].split(">")[0];
|
|
49 |
tagnamn=tagnamn.toLowerCase();
|
|
50 |
var rest=y[i].split(">")[1];
|
|
51 |
if(niva<=oldniva){
|
|
52 |
var tabort=oldniva-niva+1;
|
|
53 |
for (var j=0;j<tabort;j++){objname=objname.substring(0,objname.lastIndexOf("."))}
|
|
54 |
};
|
|
55 |
objname+="."+tagnamn;
|
|
56 |
var pobject=objname.substring(0,objname.lastIndexOf("."));
|
|
57 |
if (eval("typeof "+pobject) != "object"){preeval+=pobject+"={value:"+pobject+"};\n"};
|
|
58 |
var objlast=objname.substring(objname.lastIndexOf(".")+1);
|
|
59 |
var already=false;
|
|
60 |
for (k in eval(pobject)){if(k==objlast){already=true}};
|
|
61 |
var onlywhites=true;
|
|
62 |
for(var s=0;s<rest.length;s+=3){
|
|
63 |
if(rest.charAt(s)!="%"){onlywhites=false}
|
|
64 |
};
|
|
65 |
if (rest!="" && !onlywhites){
|
|
66 |
if(rest/1!=rest){
|
|
67 |
rest="'"+rest.replace(/\'/g,"\\'")+"'";
|
|
68 |
rest=rest.replace(/\*\$\*\*\*/g,"</");
|
|
69 |
rest=rest.replace(/\*\$\*\*/g,"<");
|
|
70 |
rest=rest.replace(/\*\*\$\*/g,">")
|
|
71 |
}
|
|
72 |
}
|
|
73 |
else {rest="{}"};
|
|
74 |
if(rest.charAt(0)=="'"){rest='unescape('+rest+')'};
|
|
75 |
if (already && !eval(objname+".sort")){preeval+=objname+"=["+objname+"];\n"};
|
|
76 |
var before="=";after="";
|
|
77 |
if (already){before=".push(";after=")"};
|
|
78 |
var toeval=preeval+objname+before+rest+after;
|
|
79 |
eval(toeval);
|
|
80 |
if(eval(objname+".sort")){objname+="["+eval(objname+".length-1")+"]"};
|
|
81 |
oldniva=niva
|
|
82 |
};
|
|
83 |
return this.xmlobject
|
|
84 |
},
|
|
85 |
show_json_structure:function(obj,debug,l){
|
|
86 |
var x='';
|
|
87 |
if (obj.sort){x+="[\n"} else {x+="{\n"};
|
|
88 |
for (var i in obj){
|
|
89 |
if (!obj.sort){x+=i+":"};
|
|
90 |
if (typeof obj[i] == "object"){
|
|
91 |
x+=this.show_json_structure(obj[i],false,1)
|
|
92 |
}
|
|
93 |
else {
|
|
94 |
if(typeof obj[i]=="function"){
|
|
95 |
var v=obj[i]+"";
|
|
96 |
//v=v.replace(/\t/g,"");
|
|
97 |
x+=v
|
|
98 |
}
|
|
99 |
else if(typeof obj[i]!="string"){x+=obj[i]+",\n"}
|
|
100 |
else {x+="'"+obj[i].replace(/\'/g,"\\'").replace(/\n/g,"\\n").replace(/\t/g,"\\t").replace(/\r/g,"\\r")+"',\n"}
|
|
101 |
}
|
|
102 |
};
|
|
103 |
if (obj.sort){x+="],\n"} else {x+="},\n"};
|
|
104 |
if (!l){
|
|
105 |
x=x.substring(0,x.lastIndexOf(","));
|
|
106 |
x=x.replace(new RegExp(",\n}","g"),"\n}");
|
|
107 |
x=x.replace(new RegExp(",\n]","g"),"\n]");
|
|
108 |
var y=x.split("\n");x="";
|
|
109 |
var lvl=0;
|
|
110 |
for (var i=0;i<y.length;i++){
|
|
111 |
if(y[i].indexOf("}")>=0 || y[i].indexOf("]")>=0){lvl--};
|
|
112 |
tabs="";for(var j=0;j<lvl;j++){tabs+="\t"};
|
|
113 |
x+=tabs+y[i]+"\n";
|
|
114 |
if(y[i].indexOf("{")>=0 || y[i].indexOf("[")>=0){lvl++}
|
|
115 |
};
|
|
116 |
if(debug=="html"){
|
|
117 |
x=x.replace(/</g,"<").replace(/>/g,">");
|
|
118 |
x=x.replace(/\n/g,"<BR>").replace(/\t/g," ")
|
|
119 |
};
|
|
120 |
if (debug=="compact"){x=x.replace(/\n/g,"").replace(/\t/g,"")}
|
|
121 |
};
|
|
122 |
return x
|
|
123 |
},
|
|
124 |
no_fast_endings:function(x){
|
|
125 |
x=x.split("/>");
|
|
126 |
for (var i=1;i<x.length;i++){
|
|
127 |
var t=x[i-1].substring(x[i-1].lastIndexOf("<")+1).split(" ")[0];
|
|
128 |
x[i]="></"+t+">"+x[i]
|
|
129 |
} ;
|
|
130 |
x=x.join("");
|
|
131 |
return x
|
|
132 |
},
|
|
133 |
attris_to_tags: function(x){
|
|
134 |
var d=' ="\''.split("");
|
|
135 |
x=x.split(">");
|
|
136 |
for (var i=0;i<x.length;i++){
|
|
137 |
var temp=x[i].split("<");
|
|
138 |
for (var r=0;r<4;r++){temp[0]=temp[0].replace(new RegExp(d[r],"g"),"_jsonconvtemp"+r+"_")};
|
|
139 |
if(temp[1]){
|
|
140 |
temp[1]=temp[1].replace(/'/g,'"');
|
|
141 |
temp[1]=temp[1].split('"');
|
|
142 |
for (var j=1;j<temp[1].length;j+=2){
|
|
143 |
for (var r=0;r<4;r++){temp[1][j]=temp[1][j].replace(new RegExp(d[r],"g"),"_jsonconvtemp"+r+"_")}
|
|
144 |
};
|
|
145 |
temp[1]=temp[1].join('"')
|
|
146 |
};
|
|
147 |
x[i]=temp.join("<")
|
|
148 |
};
|
|
149 |
x=x.join(">");
|
|
150 |
x=x.replace(/ ([^=]*)=([^ |>]*)/g,"><$1>$2</$1");
|
|
151 |
x=x.replace(/>"/g,">").replace(/"</g,"<");
|
|
152 |
for (var r=0;r<4;r++){x=x.replace(new RegExp("_jsonconvtemp"+r+"_","g"),d[r])} ;
|
|
153 |
return x
|
|
154 |
}
|
|
155 |
};
|
|
156 |
|
|
157 |
|
|
158 |
if(!Array.prototype.push){
|
|
159 |
Array.prototype.push=function(x){
|
|
160 |
this[this.length]=x;
|
|
161 |
return true
|
|
162 |
}
|
|
163 |
};
|
|
164 |
|
|
165 |
if (!Array.prototype.pop){
|
|
166 |
Array.prototype.pop=function(){
|
|
167 |
var response = this[this.length-1];
|
|
168 |
this.length--;
|
|
169 |
return response
|
|
170 |
}
|
|
171 |
};
|
|
172 |
|