home *** CD-ROM | disk | FTP | other *** search
/ Dynamic HTML in Action / Dynamicke-HTML-v-akci-covermount.bin / XML / TUTORIAL / DOMFORMATTER2.JS < prev    next >
Encoding:
Text File  |  1998-11-11  |  5.2 KB  |  174 lines

  1. function ShowDI(doc,xmlid)
  2. {
  3.     var w = window.open("","","resizable,scrollbars,width=640,height=480");
  4.     w.title = "XML Data Island";
  5.     w.document.open();
  6.     w.document.write("<STYLE>");
  7. w.document.write("  .html    {color:blue; font-weight:bold; font-size:9pt;font-family:Arial}");
  8.     w.document.write("  .xml    {font-size:9pt;font-family:Arial}");
  9.     w.document.write("  .tag    {color:red; font-weight:bold; }");
  10.     w.document.write("  .attribute{color:blue; font-weight:bold; }");
  11.     w.document.write("  .attrvalue{color:darkorchid; font-weight:bold; }");
  12.     w.document.write("  .comment{color:green; font-weight:bold; }");
  13.     w.document.write("</STYLE>");
  14. w.document.write("<DL class=html><HTML></DL>");
  15. w.document.write("<DL class=html><HEAD></DL>");
  16. w.document.write("<DL class=html><TITLE>HTML Document with Data Island</TITLE></DL>");
  17. w.document.write("<DL class=html></HEAD></DL>");
  18.     w.document.write("<DL class=html><BODY></DL>");
  19. w.document.write("<DL class=xml><DD class=tag><XML ID='" + xmlid + "'></DD></DL>");
  20. var xml = DumpTree(doc,0);
  21. w.document.write(xml);
  22. w.document.write("<DL class=xml><DD class=tag></XML></DD></DL>");
  23. w.document.write("<DL class=html></BODY></DL>");
  24. w.document.write("<DL class=html></HTML></DL>");
  25.     w.document.close();
  26. }
  27.  
  28. function ShowXML(doc)
  29. {
  30.     var w = window.open("","","resizable,scrollbars,width=640,height=480");
  31.     w.title = "XML Data";
  32.     w.document.open();
  33.     w.document.write("<STYLE>");
  34.     w.document.write("  .xml    {font-size:9pt;font-family:Arial}");
  35.     w.document.write("  .tag    {color:red; font-weight:bold; }");
  36.     w.document.write("  .attribute{color:blue; font-weight:bold; }");
  37.     w.document.write("  .attrvalue{color:darkorchid; font-weight:bold; }");
  38.     w.document.write("  .comment{color:green; font-weight:bold; }");
  39.     w.document.write("</STYLE>");
  40.     var xml = DumpTree(doc,0);
  41.     w.document.write(xml);
  42.     w.document.close();
  43. }
  44.  
  45. function qname(node)
  46. {
  47.     var ns = node.nameSpace;
  48.     if (ns == null)
  49.         return node.nodeName;
  50.  
  51.     var prefix = ns.getAttribute("prefix");
  52.     return prefix + ":" + node.nodeName;
  53. }
  54.  
  55. function aqname(node, attr)
  56. {
  57.     var ns2 = attr.nameSpace;
  58.     if (ns2 == null)
  59.         return attr.nodeName;
  60.  
  61.     var ns = node.nameSpace;
  62.     if (ns && (ns.getAttribute("ns") == ns2.getAttribute("ns")))
  63.         return attr.nodeName;
  64.  
  65.     var prefix;
  66.     prefix = ns2.getAttribute("prefix");
  67.  
  68.     return prefix + ":" + node.nodeName;
  69. }
  70.  
  71. function DumpTree(node,i,ns)
  72. {
  73.     if (node == null) return "";
  74.  
  75.     var type = node.nodeType;
  76. result = "<DL class=xml><DD>";
  77.  
  78. if (type == 1 || type == 6 || type == 8 )
  79. {
  80. result += node.nodeValue;
  81. result += "</DD></DL>";
  82. return result;
  83. }
  84. if (type == 2) {
  85. result += "<span class=comment><!--" 
  86. + node.nodeValue + "--></span>";
  87. result += "</DD></DL>"
  88. return result;
  89. }
  90. if (type == 3)
  91. {
  92. var en = node.childNodes;
  93. for (var child = en.currentNode(); child != null; child = en.nextNode())
  94. {
  95. if (child.nodeType != 9)
  96. result += DumpTree(child,i);
  97. }
  98. result += "</DD></DL>"
  99. return result;
  100. }
  101. if (type == 5)
  102. {
  103. result += "<span class=pi><?" + node.nodeName + " "
  104. + node.nodeValue + "?></span>";
  105. result += "</DD></DL>"
  106. return result;
  107. }
  108.     if (type == 4)
  109.     {
  110.         result += "<span class=decl><!DOCTYPE " + qname(node);
  111.         var pubid = node.getAttribute("publicid");
  112.         var sys = node.getAttribute("SYSTEM");
  113.         if (pubid != "" && pubid != null)
  114.             result += " PUBLIC " + pubid;
  115.         else if (sys != "" && sys != null)
  116.             result += " SYSTEM";
  117.         if (sys != "" && sys != null) 
  118.             result += " " + sys;
  119.  
  120.         var value = ""; // node.nodeValue;
  121.         if (value != "")
  122.         {
  123.             result += " [" + value + "]";
  124.         }
  125.         result += "></span></DD></DL>";
  126.         return result;
  127.     }
  128.  
  129. var num = node.childNodes.length;
  130.     var children = node.childNodes;
  131.     var num = children.length;
  132.     var empty = "";
  133.     if (num == 0) 
  134.     {
  135.         empty = "/";
  136.         if (type == 11 || type == 7)
  137.         {
  138.             empty = "?";
  139.         }
  140.     }
  141.     if (type != 3)
  142.     {
  143.         result += "<span class=tag><";
  144.         if (type == 11 || type == 7) result += "?"
  145.         result = result + qname(node) + "</span>";
  146.     }
  147.  
  148.     if (type == 0 || type == 7 || type == 11)
  149.     {
  150.         var attrs = node.attributes;
  151.         var na = attrs.length;
  152.         for (i = 0; i < na; i++) {
  153.             var a = attrs.item(i);
  154.             result += " <span class=attribute>" + aqname(node,a) + "</span>=<span class=attrvalue>\"" + a.nodeValue + "\"</span>";
  155.         }
  156.     }
  157.     if (type != 3)
  158.         result += "<span class=tag>" + empty + "></span>";
  159.  
  160.     if (num > 0) {
  161.         for (var child = children.currentNode(); child != null; child = children.nextNode())
  162.         {
  163.             result += "\n";
  164.             if (child.nodeType != 9)
  165.                 result += DumpTree(child,i+1);
  166.         }
  167.         if (type != 3)
  168.             result +=  "<span class=tag></" + qname(node) + "></span>\n";
  169.     }
  170.     result += "</DD></DL>"
  171.     return result;
  172. }
  173.  
  174.