parser.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. define("dojox/xml/parser", ['dojo/_base/kernel', 'dojo/_base/lang', 'dojo/_base/array', 'dojo/_base/window', 'dojo/_base/sniff'], function(dojo){
  2. dojo.getObject("xml.parser", true, dojox);
  3. //DOM type to int value for reference.
  4. //Ints make for more compact code than full constant names.
  5. //ELEMENT_NODE = 1;
  6. //ATTRIBUTE_NODE = 2;
  7. //TEXT_NODE = 3;
  8. //CDATA_SECTION_NODE = 4;
  9. //ENTITY_REFERENCE_NODE = 5;
  10. //ENTITY_NODE = 6;
  11. //PROCESSING_INSTRUCTION_NODE = 7;
  12. //COMMENT_NODE = 8;
  13. //DOCUMENT_NODE = 9;
  14. //DOCUMENT_TYPE_NODE = 10;
  15. //DOCUMENT_FRAGMENT_NODE = 11;
  16. //NOTATION_NODE = 12;
  17. dojox.xml.parser.parse = function(/*String?*/ str, /*String?*/ mimetype){
  18. // summary:
  19. // cross-browser implementation of creating an XML document object from null, empty string, and XML text..
  20. //
  21. // str:
  22. // Optional text to create the document from. If not provided, an empty XML document will be created.
  23. // If str is empty string "", then a new empty document will be created.
  24. // mimetype:
  25. // Optional mimetype of the text. Typically, this is text/xml. Will be defaulted to text/xml if not provided.
  26. var _document = dojo.doc;
  27. var doc;
  28. mimetype = mimetype || "text/xml";
  29. if(str && dojo.trim(str) && "DOMParser" in dojo.global){
  30. //Handle parsing the text on Mozilla based browsers etc..
  31. var parser = new DOMParser();
  32. doc = parser.parseFromString(str, mimetype);
  33. var de = doc.documentElement;
  34. var errorNS = "http://www.mozilla.org/newlayout/xml/parsererror.xml";
  35. if(de.nodeName == "parsererror" && de.namespaceURI == errorNS){
  36. var sourceText = de.getElementsByTagNameNS(errorNS, 'sourcetext')[0];
  37. if(sourceText){
  38. sourceText = sourceText.firstChild.data;
  39. }
  40. throw new Error("Error parsing text " + de.firstChild.data + " \n" + sourceText);
  41. }
  42. return doc;
  43. }else if("ActiveXObject" in dojo.global){
  44. //Handle IE.
  45. var ms = function(n){ return "MSXML" + n + ".DOMDocument"; };
  46. var dp = ["Microsoft.XMLDOM", ms(6), ms(4), ms(3), ms(2)];
  47. dojo.some(dp, function(p){
  48. try{
  49. doc = new ActiveXObject(p);
  50. }catch(e){ return false; }
  51. return true;
  52. });
  53. if(str && doc){
  54. doc.async = false;
  55. doc.loadXML(str);
  56. var pe = doc.parseError;
  57. if(pe.errorCode !== 0){
  58. throw new Error("Line: " + pe.line + "\n" +
  59. "Col: " + pe.linepos + "\n" +
  60. "Reason: " + pe.reason + "\n" +
  61. "Error Code: " + pe.errorCode + "\n" +
  62. "Source: " + pe.srcText);
  63. }
  64. }
  65. if(doc){
  66. return doc; //DOMDocument
  67. }
  68. }else if(_document.implementation && _document.implementation.createDocument){
  69. if(str && dojo.trim(str) && _document.createElement){
  70. //Everyone else that we couldn't get to work. Fallback case.
  71. // FIXME: this may change all tags to uppercase!
  72. var tmp = _document.createElement("xml");
  73. tmp.innerHTML = str;
  74. var xmlDoc = _document.implementation.createDocument("foo", "", null);
  75. dojo.forEach(tmp.childNodes, function(child){
  76. xmlDoc.importNode(child, true);
  77. });
  78. return xmlDoc; // DOMDocument
  79. }else{
  80. return _document.implementation.createDocument("", "", null); // DOMDocument
  81. }
  82. }
  83. return null; // null
  84. };
  85. dojox.xml.parser.textContent = function(/*Node*/node, /*String?*/text){
  86. // summary:
  87. // Implementation of the DOM Level 3 attribute; scan node for text
  88. // description:
  89. // Implementation of the DOM Level 3 attribute; scan node for text
  90. // This function can also update the text of a node by replacing all child
  91. // content of the node.
  92. // node:
  93. // The node to get the text off of or set the text on.
  94. // text:
  95. // Optional argument of the text to apply to the node.
  96. if(arguments.length>1){
  97. var _document = node.ownerDocument || dojo.doc; //Preference is to get the node owning doc first or it may fail
  98. dojox.xml.parser.replaceChildren(node, _document.createTextNode(text));
  99. return text; // String
  100. }else{
  101. if(node.textContent !== undefined){ //FF 1.5 -- remove?
  102. return node.textContent; // String
  103. }
  104. var _result = "";
  105. if(node){
  106. dojo.forEach(node.childNodes, function(child){
  107. switch(child.nodeType){
  108. case 1: // ELEMENT_NODE
  109. case 5: // ENTITY_REFERENCE_NODE
  110. _result += dojox.xml.parser.textContent(child);
  111. break;
  112. case 3: // TEXT_NODE
  113. case 2: // ATTRIBUTE_NODE
  114. case 4: // CDATA_SECTION_NODE
  115. _result += child.nodeValue;
  116. }
  117. });
  118. }
  119. return _result; // String
  120. }
  121. };
  122. dojox.xml.parser.replaceChildren = function(/*Element*/node, /*Node || Array*/ newChildren){
  123. // summary:
  124. // Removes all children of node and appends newChild. All the existing
  125. // children will be destroyed.
  126. // description:
  127. // Removes all children of node and appends newChild. All the existing
  128. // children will be destroyed.
  129. // node:
  130. // The node to modify the children on
  131. // newChildren:
  132. // The children to add to the node. It can either be a single Node or an
  133. // array of Nodes.
  134. var nodes = [];
  135. if(dojo.isIE){
  136. dojo.forEach(node.childNodes, function(child){
  137. nodes.push(child);
  138. });
  139. }
  140. dojox.xml.parser.removeChildren(node);
  141. dojo.forEach(nodes, dojo.destroy);
  142. if(!dojo.isArray(newChildren)){
  143. node.appendChild(newChildren);
  144. }else{
  145. dojo.forEach(newChildren, function(child){
  146. node.appendChild(child);
  147. });
  148. }
  149. };
  150. dojox.xml.parser.removeChildren = function(/*Element*/node){
  151. // summary:
  152. // removes all children from node and returns the count of children removed.
  153. // The children nodes are not destroyed. Be sure to call dojo.destroy on them
  154. // after they are not used anymore.
  155. // node:
  156. // The node to remove all the children from.
  157. var count = node.childNodes.length;
  158. while(node.hasChildNodes()){
  159. node.removeChild(node.firstChild);
  160. }
  161. return count; // int
  162. };
  163. dojox.xml.parser.innerXML = function(/*Node*/node){
  164. // summary:
  165. // Implementation of MS's innerXML function.
  166. // node:
  167. // The node from which to generate the XML text representation.
  168. if(node.innerXML){
  169. return node.innerXML; // String
  170. }else if(node.xml){
  171. return node.xml; // String
  172. }else if(typeof XMLSerializer != "undefined"){
  173. return (new XMLSerializer()).serializeToString(node); // String
  174. }
  175. return null;
  176. };
  177. return dojox.xml.parser;
  178. });