-
Notifications
You must be signed in to change notification settings - Fork 21
/
xml2json_demo.html
79 lines (65 loc) · 1.96 KB
/
xml2json_demo.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
<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">
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;
}
var convertXml = function() {
document.getElementById('json').value = "If you can still see this text, then something didn't work...";
var xml = document.getElementById('xml').value;
var json = xml2json( parseXml(xml), " ");
showJson(json);
}
var convertJson = function() {
document.getElementById('xml').value = "If you can still see this text, then something didn't work...";
var json = document.getElementById('json').value;
var xml = json2xml( json, " ");
showXml(xml);
}
window.onload = function() { convertXml(); }
function showJson(s) { document.getElementById("json").value = s.replace(/&/g, "&").replace(/</g,"<").replace(/>/g,">"); }
function showXml(s) { document.getElementById("xml").value = s; s.replace(/&/g, "&").replace(/</g,"<").replace(/>/g,">"); }
</script>
</head>
<body>
<table border="0">
<tr>
<td>
<p>XML</p>
<textarea rows="30" cols="80" id="xml"><e>Hello World</e></textarea>
</td>
<td>
<input type="submit" value="->" onclick="convertXml();" />
<br />
<input type="submit" value="<-" onclick="convertJson();" />
</td>
<td>
<p>JSON</p>
<textarea rows="30" cols="80" id="json">...</textarea>
</td>
</table>
<br />
<br />
</body>
</html>