Exercises
We'll use the JavaScript that was used in Internet Explorer 4 for transforming an XML file.
The script seen below in Listing 16.15 has been written in an outdated syntax of the DOM. Rewrite the script so it conforms to the recommended spec of the DOM.
Listing 16.15 A Script to Update
1: <SCRIPT LANGUAGE="JScript" FOR="window" EVENT="onload">
2: document.write("<HTML><HEAD><TITLE>My favorite
musicians</TITLE></HEAD>\n");
3: document.write("<BODY><H2>My favorite musicians</H2><HR>\n") ;
4: var xml = new ActiveXObject("msxml");
5: xml.URL = "file:///c|/xmlex/musicians.xml";
6: var docroot = xml.root;
7: output_doc(docroot);
8:
9: function traverse(elem)
10: {var i;
11: if (elem.children != null)
12: {
13: for (i=0; i < elem.children.length; i++)
14: output_doc(elem.children.item(i));
15: }
16: }
17:
18: function output_doc(elem)
19: {
20: if (elem.type == 0)
21: {
22: if (elem.tagName == "MUSICIANS")
23: {
24: document.write("<TABLE
BORDER='1' CELLPADDING='5'>");
25: traverse(elem);
26: document.write("</TABLE>");
27: }
28: else if (elem.tagName == "MUSICIAN")
29: {
30: document.write("<TR>");
31: traverse(elem);
32: document.write("</TR>");
33: }
34: else
35: {
36: document.write("<TD>");
37: traverse(elem);
38: document.write("</TD>");
39: }
40:
41: }
42: else if (elem.type==1)
43: document.write(elem.text);
44: else
45: alert("Unknown type encountered");
46: }
47: </SCRIPT>