-
Notifications
You must be signed in to change notification settings - Fork 1
/
XMLDisplay.js
263 lines (253 loc) · 10.2 KB
/
XMLDisplay.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* Copyright (c) 2007 Lev Muchnik <[email protected]>. All rights reserved.
* You may copy and modify this script as long as the above copyright notice,
* this condition and the following disclaimer is left intact.
* This software is provided by the author "AS IS" and no warranties are
* implied, including fitness for a particular purpose. In no event shall
* the author be liable for any damages arising in any way out of the use
* of this software, even if advised of the possibility of such damage.
* $Date: 2007-10-03 19:08:15 -0700 (Wed, 03 Oct 2007) $
*/
function LoadXML(ParentElementID,URL)
{
var xmlHolderElement = GetParentElement(ParentElementID);
if (xmlHolderElement==null) { return false; }
ToggleElementVisibility(xmlHolderElement);
return RequestURL(URL,URLReceiveCallback,ParentElementID);
}
function LoadXMLDom(ParentElementID,xmlDoc)
{
if (xmlDoc) {
var xmlHolderElement = GetParentElement(ParentElementID);
if (xmlHolderElement==null) { return false; }
while (xmlHolderElement.childNodes.length) { xmlHolderElement.removeChild(xmlHolderElement.childNodes.item(xmlHolderElement.childNodes.length-1)); }
var Result = ShowXML(xmlHolderElement,xmlDoc.documentElement,0);
var ReferenceElement = document.createElement('div');
var Link = document.createElement('a');
Link.setAttribute('href','http://www.levmuchnik.net/Content/ProgrammingTips/WEB/XMLDisplay/DisplayXMLFileWithJavascript.html');
var TextNode = document.createTextNode('Source: Lev Muchnik');
Link.appendChild(TextNode);
xmlHolderElement.appendChild(Link);
return Result;
}
else { return false; }
}
function LoadXMLString(ParentElementID,XMLString)
{
xmlDoc = CreateXMLDOM(XMLString);
return LoadXMLDom(ParentElementID,xmlDoc) ;
}
////////////////////////////////////////////////////////////
// HELPER FUNCTIONS - SHOULD NOT BE DIRECTLY CALLED BY USERS
////////////////////////////////////////////////////////////
function GetParentElement(ParentElementID)
{
if (typeof(ParentElementID)=='string') { return document.getElementById(ParentElementID); }
else if (typeof(ParentElementID)=='object') { return ParentElementID;}
else { return null; }
}
function URLReceiveCallback(httpRequest,xmlHolderElement)
{
try {
if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {
var xmlDoc = httpRequest.responseXML;
if (xmlHolderElement && xmlHolderElement!=null) {
xmlHolderElement.innerHTML = '';
return LoadXMLDom(xmlHolderElement,xmlDoc);
}
} else {
return false;
}
}
}
catch( e ) {
return false;
}
}
function RequestURL(url,callback,ExtraData) { // based on: http://developer.mozilla.org/en/docs/AJAX:Getting_Started
var httpRequest;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
if (httpRequest.overrideMimeType) { httpRequest.overrideMimeType('text/xml'); }
}
else if (window.ActiveXObject) { // IE
try { httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); }
catch (e) {
try { httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); }
catch (e) {}
}
}
if (!httpRequest) { return false; }
httpRequest.onreadystatechange = function() { callback(httpRequest,ExtraData); };
httpRequest.open('GET', url, true);
httpRequest.send('');
return true;
}
function CreateXMLDOM(XMLStr)
{
if (window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.loadXML(XMLStr);
return xmlDoc;
}
else if (document.implementation && document.implementation.createDocument) {
var parser=new DOMParser();
return parser.parseFromString(XMLStr,"text/xml");
}
else {
return null;
}
}
var IDCounter = 1;
var NestingIndent = 15;
function ShowXML(xmlHolderElement,RootNode,indent)
{
if (RootNode==null || xmlHolderElement==null) { return false; }
var Result = true;
var TagEmptyElement = document.createElement('div');
TagEmptyElement.className = 'Element emh-xml-viewer';
TagEmptyElement.style.position = 'relative';
TagEmptyElement.style.left = NestingIndent+'px';
if (RootNode.childNodes.length==0) {
var ClickableElement = AddTextNode(TagEmptyElement,'','Clickable emh-xml-viewer') ;
ClickableElement.id = 'div_empty_' + IDCounter;
AddTextNode(TagEmptyElement,'<','Utility emh-xml-viewer') ;
AddTextNode(TagEmptyElement,RootNode.nodeName ,'NodeName emh-xml-viewer')
for (var i = 0; RootNode.attributes && i < RootNode.attributes.length; ++i) {
CurrentAttribute = RootNode.attributes.item(i);
AddTextNode(TagEmptyElement,' ' + CurrentAttribute.nodeName ,'AttributeName emh-xml-viewer') ;
AddTextNode(TagEmptyElement,'=','Utility emh-xml-viewer') ;
AddTextNode(TagEmptyElement,'"' + CurrentAttribute.nodeValue + '"','AttributeValue emh-xml-viewer') ;
}
AddTextNode(TagEmptyElement,' />') ;
xmlHolderElement.appendChild(TagEmptyElement);
//SetVisibility(TagEmptyElement,true);
}
else { // mo child nodes
var ClickableElement = AddTextNode(TagEmptyElement,'+','Clickable emh-xml-viewer') ;
ClickableElement.onclick = function() {ToggleElementVisibility(this); }
ClickableElement.id = 'div_empty_' + IDCounter;
AddTextNode(TagEmptyElement,'<','Utility emh-xml-viewer') ;
AddTextNode(TagEmptyElement,RootNode.nodeName ,'NodeName emh-xml-viewer')
for (var i = 0; RootNode.attributes && i < RootNode.attributes.length; ++i) {
CurrentAttribute = RootNode.attributes.item(i);
AddTextNode(TagEmptyElement,' ' + CurrentAttribute.nodeName ,'AttributeName emh-xml-viewer') ;
AddTextNode(TagEmptyElement,'=','Utility emh-xml-viewer') ;
AddTextNode(TagEmptyElement,'"' + CurrentAttribute.nodeValue + '"','AttributeValue emh-xml-viewer') ;
}
AddTextNode(TagEmptyElement,'> </','Utility emh-xml-viewer') ;
AddTextNode(TagEmptyElement,RootNode.nodeName,'NodeName emh-xml-viewer') ;
AddTextNode(TagEmptyElement,'>','Utility emh-xml-viewer') ;
xmlHolderElement.appendChild(TagEmptyElement);
SetVisibility(TagEmptyElement,false);
//----------------------------------------------
var TagElement = document.createElement('div');
TagElement.className = 'Element emh-xml-viewer';
TagElement.style.position = 'relative';
TagElement.style.left = NestingIndent+'px';
ClickableElement = AddTextNode(TagElement,'-','Clickable emh-xml-viewer') ;
ClickableElement.onclick = function() {ToggleElementVisibility(this); }
ClickableElement.id = 'div_content_' + IDCounter;
++IDCounter;
AddTextNode(TagElement,'<','Utility emh-xml-viewer') ;
AddTextNode(TagElement,RootNode.nodeName ,'NodeName emh-xml-viewer') ;
for (var i = 0; RootNode.attributes && i < RootNode.attributes.length; ++i) {
CurrentAttribute = RootNode.attributes.item(i);
AddTextNode(TagElement,' ' + CurrentAttribute.nodeName ,'AttributeName emh-xml-viewer') ;
AddTextNode(TagElement,'=','Utility emh-xml-viewer') ;
AddTextNode(TagElement,'"' + CurrentAttribute.nodeValue + '"','AttributeValue emh-xml-viewer') ;
}
AddTextNode(TagElement,'>','Utility emh-xml-viewer') ;
TagElement.appendChild(document.createElement('br'));
var NodeContent = null;
for (var i = 0; RootNode.childNodes && i < RootNode.childNodes.length; ++i) {
if (RootNode.childNodes.item(i).nodeName != '#text') {
Result &= ShowXML(TagElement,RootNode.childNodes.item(i),indent+1);
}
else {
NodeContent =RootNode.childNodes.item(i).nodeValue;
}
}
if (RootNode.nodeValue) {
NodeContent = RootNode.nodeValue;
}
if (NodeContent) {
var ContentElement = document.createElement('div');
ContentElement.style.position = 'relative';
ContentElement.style.left = NestingIndent+'px';
AddTextNode(ContentElement,NodeContent ,'NodeValue emh-xml-viewer') ;
TagElement.appendChild(ContentElement);
}
AddTextNode(TagElement,' </','Utility emh-xml-viewer') ;
AddTextNode(TagElement,RootNode.nodeName,'NodeName emh-xml-viewer') ;
AddTextNode(TagElement,'>','Utility emh-xml-viewer') ;
xmlHolderElement.appendChild(TagElement);
}
// if (indent==0) { ToggleElementVisibility(TagElement.childNodes(0)); } - uncomment to collapse the external element
return Result;
}
function AddTextNode(ParentNode,Text,Class)
{
NewNode = document.createElement('span');
if (Class) { NewNode.className = Class;}
if (Text) { NewNode.appendChild(document.createTextNode(Text)); }
if (ParentNode) { ParentNode.appendChild(NewNode); }
return NewNode;
}
function CompatibleGetElementByID(id)
{
if (!id) { return null; }
if (document.getElementById) { // DOM3 = IE5, NS6
return document.getElementById(id);
}
else {
if (document.layers) { // Netscape 4
return document.id;
}
else { // IE 4
return document.all.id;
}
}
}
function SetVisibility(HTMLElement,Visible)
{
if (!HTMLElement) { return; }
var VisibilityStr = (Visible) ? 'block' : 'none';
if (document.getElementById) { // DOM3 = IE5, NS6
HTMLElement.style.display =VisibilityStr;
}
else {
if (document.layers) { // Netscape 4
HTMLElement.display = VisibilityStr;
}
else { // IE 4
HTMLElement.id.style.display = VisibilityStr;
}
}
}
function ToggleElementVisibility(Element)
{
if (!Element|| !Element.id) { return; }
try {
ElementType = Element.id.slice(0,Element.id.lastIndexOf('_')+1);
ElementID = parseInt(Element.id.slice(Element.id.lastIndexOf('_')+1));
}
catch(e) { return ; }
var ElementToHide = null;
var ElementToShow= null;
if (ElementType=='div_content_') {
ElementToHide = 'div_content_' + ElementID;
ElementToShow = 'div_empty_' + ElementID;
}
else if (ElementType=='div_empty_') {
ElementToShow= 'div_content_' + ElementID;
ElementToHide = 'div_empty_' + ElementID;
}
ElementToHide = CompatibleGetElementByID(ElementToHide);
ElementToShow = CompatibleGetElementByID(ElementToShow);
if (ElementToHide) { ElementToHide = ElementToHide.parentNode;}
if (ElementToShow) { ElementToShow = ElementToShow.parentNode;}
SetVisibility(ElementToHide,false);
SetVisibility(ElementToShow,true);
}