xmlextras.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //<script>
  2. //////////////////
  3. // Helper Stuff //
  4. //////////////////
  5. // used to find the Automation server name
  6. function getDomDocumentPrefix() {
  7. if (getDomDocumentPrefix.prefix)
  8. return getDomDocumentPrefix.prefix;
  9. var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
  10. var o;
  11. for (var i = 0; i < prefixes.length; i++) {
  12. try {
  13. // try to create the objects
  14. o = new ActiveXObject(prefixes[i] + ".DomDocument");
  15. return getDomDocumentPrefix.prefix = prefixes[i];
  16. }
  17. catch (ex) {};
  18. }
  19. throw new Error("Could not find an installed XML parser");
  20. }
  21. function getXmlHttpPrefix() {
  22. if (getXmlHttpPrefix.prefix)
  23. return getXmlHttpPrefix.prefix;
  24. var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
  25. var o;
  26. for (var i = 0; i < prefixes.length; i++) {
  27. try {
  28. // try to create the objects
  29. o = new ActiveXObject(prefixes[i] + ".XmlHttp");
  30. return getXmlHttpPrefix.prefix = prefixes[i];
  31. }
  32. catch (ex) {};
  33. }
  34. throw new Error("Could not find an installed XML parser");
  35. }
  36. //////////////////////////
  37. // Start the Real stuff //
  38. //////////////////////////
  39. // XmlHttp factory
  40. function XmlHttp() {}
  41. XmlHttp.create = function () {
  42. try {
  43. if (window.XMLHttpRequest) {
  44. var req = new XMLHttpRequest();
  45. // some versions of Moz do not support the readyState property
  46. // and the onreadystate event so we patch it!
  47. if (req.readyState == null) {
  48. req.readyState = 1;
  49. req.addEventListener("load", function () {
  50. req.readyState = 4;
  51. if (typeof req.onreadystatechange == "function")
  52. req.onreadystatechange();
  53. }, false);
  54. }
  55. return req;
  56. }
  57. if (window.ActiveXObject) {
  58. return new ActiveXObject(getXmlHttpPrefix() + ".XmlHttp");
  59. }
  60. }
  61. catch (ex) {}
  62. // fell through
  63. throw new Error("Your browser does not support XmlHttp objects");
  64. };
  65. // XmlDocument factory
  66. function XmlDocument() {}
  67. XmlDocument.create = function () {
  68. try {
  69. // DOM2
  70. if (document.implementation && document.implementation.createDocument) {
  71. var doc = document.implementation.createDocument("", "", null);
  72. // some versions of Moz do not support the readyState property
  73. // and the onreadystate event so we patch it!
  74. if (doc.readyState == null) {
  75. doc.readyState = 1;
  76. doc.addEventListener("load", function () {
  77. doc.readyState = 4;
  78. if (typeof doc.onreadystatechange == "function")
  79. doc.onreadystatechange();
  80. }, false);
  81. }
  82. return doc;
  83. }
  84. if (window.ActiveXObject)
  85. return new ActiveXObject(getDomDocumentPrefix() + ".DomDocument");
  86. }
  87. catch (ex) {}
  88. throw new Error("Your browser does not support XmlDocument objects");
  89. };
  90. // Create the loadXML method and xml getter for Mozilla
  91. if (window.DOMParser &&
  92. window.XMLSerializer &&
  93. window.Node && Node.prototype && Node.prototype.__defineGetter__) {
  94. // XMLDocument did not extend the Document interface in some versions
  95. // of Mozilla. Extend both!
  96. XMLDocument.prototype.loadXML =
  97. Document.prototype.loadXML = function (s) {
  98. // parse the string to a new doc
  99. var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
  100. // remove all initial children
  101. while (this.hasChildNodes())
  102. this.removeChild(this.lastChild);
  103. // insert and import nodes
  104. for (var i = 0; i < doc2.childNodes.length; i++) {
  105. this.appendChild(this.importNode(doc2.childNodes[i], true));
  106. }
  107. };
  108. /*
  109. * xml getter
  110. *
  111. * This serializes the DOM tree to an XML String
  112. *
  113. * Usage: var sXml = oNode.xml
  114. *
  115. */
  116. // XMLDocument did not extend the Document interface in some versions
  117. // of Mozilla. Extend both!
  118. XMLDocument.prototype.__defineGetter__("xml", function () {
  119. return (new XMLSerializer()).serializeToString(this);
  120. });
  121. Document.prototype.__defineGetter__("xml", function () {
  122. return (new XMLSerializer()).serializeToString(this);
  123. });
  124. }