-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathxmljson_test.html
138 lines (112 loc) · 3.19 KB
/
xmljson_test.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<html>
<head>
<title> XML/JSON - Tests </title>
<script type="text/javascript" src="xml2json.js"></script>
<script type="text/javascript" src="json2xml.js"></script>
<script type="text/javascript">
var xml=[
'<e/>',
'<e>text</e>',
'<e name="value" />',
'<e name="value">text</e>',
'<e> <a>text</a> <b>text</b> </e>',
'<e> <c><a>text</a> <b>text</b></c> <d><a>text</a> <b>text</b></d> </e>',
'<e> <a>text</a> <a>text</a> </e>',
'<e> text <a>text</a> </e>',
'<a>hello</a>',
'<a x="y">hello</a>',
'<a id="a"><b id="b">hey!</b></a>',
'<a>x<c/>y</a>',
'<x u=""/>',
'<html>'+
' <head>'+
' <title>Xml/Json</title>'+
' <meta name="x" content="y" />'+
' </head>'+
' <body>'+
' </body>'+
'</html>',
'<ol class="xoxo">'+
' <li>Subject 1'+
' <ol>'+
' <li>subpoint a</li>'+
' <li>subpoint b</li>'+
' </ol>'+
' </li>'+
' <li><span>Subject 2</span>'+
' <ol compact="compact">'+
' <li>subpoint c</li>'+
' <li>subpoint d</li>'+
' </ol>'+
' </li>'+
'</ol>',
'<span class="vevent">'+
' <a class="url" href="http://www.web2con.com/">'+
' <span class="summary">Web 2.0 Conference</span>'+
' <abbr class="dtstart" title="2005-10-05">October 5</abbr>'+
' <abbr class="dtend" title="2005-10-08">7</abbr>'+
' <span class="location">Argent Hotel, San Francisco, CA</span>'+
' </a>'+
'</span>',
'<e>\n'+
' <![CDATA[ .. some data .. ]]>\n'+
'</e>',
'<e>\n'+
' <a />\n' +
' <![CDATA[ .. some data .. ]]>\n'+
' <b />\n' +
'</e>',
'<e>\n'+
' some text\n' +
' <![CDATA[ .. some data .. ]]>\n'+
' more text\n' +
'</e>',
'<e>\n'+
' some text\n' +
' <![CDATA[ .. some data .. ]]>\n'+
' <a />\n' +
'</e>',
'<e>\n'+
' <![CDATA[ .. some data .. ]]>\n'+
' <![CDATA[ .. more data .. ]]>\n'+
'</e>'
];
function parseXml(xml) {
var dom = null;
if (window.DOMParser) {
try {
dom = (new DOMParser()).parseFromString(xml, "text/xml");
}
catch (e) { dom = null; }
}
else if (window.ActiveXObject) {
try {
dom = new ActiveXObject('Microsoft.XMLDOM');
dom.async = false;
if (!dom.loadXML(xml)) // parse error ..
window.alert(dom.parseError.reason + dom.parseError.srcText);
}
catch (e) { dom = null; }
}
else
alert("oops");
return dom;
}
window.onload = function() {
var json;
eval('a = {"e":null}');
for (var i=0; i<xml.length; i++) {
show(xml[i] + "\n\n");
show( (json = xml2json(parseXml(xml[i]), " ")) + "\n\n" +
""/*json2xml(eval('json='+json))*/ );
hr();
}
}
function show(s) { document.getElementById("out").innerHTML += (s+"\n").replace(/&/g, "&").replace(/</g,"<").replace(/>/g,">").replace(/\n/g, "<br/>"); }
function hr() { document.getElementById("out").innerHTML += "<hr/>\n"; }
</script>
</head>
<body>
<pre id="out"></pre>
</body>
</html>