xgetty.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * XMLGetty - Very Very Simple XML Dom Selector Engine By XPath
  3. * - xpath
  4. */
  5. (function(){
  6. var XMLGetty = function(node){
  7. this.selectSingleNode = function(path) {
  8. if(!node) {
  9. return _NULL;
  10. }
  11. return node.selectSingleNode(path);
  12. };
  13. this.selectNodes = function(path) {
  14. if(!node) {
  15. return [];
  16. }
  17. return node.selectNodes(path);
  18. };
  19. this.getAttributeNode = function(name) {
  20. if(!node) {
  21. return _NULL;
  22. }
  23. return node.getAttributeNode(name);
  24. };
  25. this.hasChildNodes = function() {
  26. if(!node) {
  27. return _FALSE;
  28. }
  29. return node.hasChildNodes();
  30. };
  31. this.text = node? (node.textContent || node.text) : _NULL;
  32. this.type = node? node.nodeType : 0;
  33. this.name = (node && node.nodeType == 1)? (node.nodeName || "") : "";
  34. return this;
  35. };
  36. XMLGetty.prototype = {
  37. 'getValueOrDefault': function(val, defval) {
  38. if (val === "") {
  39. return defval;
  40. } else {
  41. if (typeof(defval) === 'number') {
  42. return (isNaN(val) ? 0 : parseInt(val));
  43. } else if (typeof(defval) === 'boolean') {
  44. return !!val;
  45. } else {
  46. return val;
  47. }
  48. }
  49. },
  50. 'xText': function(defval){
  51. defval = defval || "";
  52. var val = this.text;
  53. val = (val || "").trim();
  54. return this.getValueOrDefault(val, defval);
  55. },
  56. 'xAttr': function(name, defval){
  57. defval = defval || "";
  58. var attr = this.getAttributeNode(name);
  59. var val = (!attr) ? "" : attr.nodeValue.trim();
  60. return this.getValueOrDefault(val, defval);
  61. },
  62. 'xGet': function(path){
  63. return xGetty(this, path);
  64. },
  65. 'xGets': function(path){
  66. return xGetties(this, path);
  67. }
  68. };
  69. var ieXmlParsers = [
  70. "MSXML2.DOMDocument.6.0",
  71. "MSXML2.DOMDocument.5.0",
  72. "MSXML2.DOMDocument.4.0",
  73. "MSXML4.DOMDocument",
  74. "MSXML3.DOMDocument",
  75. "MSXML2.DOMDocument",
  76. "MSXML.DOMDocument",
  77. "Microsoft.XmlDom"
  78. ];
  79. /**
  80. * xCreate : Get XML DOM From XML Text
  81. * @example
  82. * var _xmlDoc = xCreate("<data><name>hopeserver</name></data>");
  83. *
  84. * @param {string} text - responseText
  85. * @return node
  86. * extend function as xText, xAttr, xGet, xGets
  87. */
  88. _WIN.xCreate = function(text) {
  89. if($tx.msie) {
  90. var xObj = (function() {
  91. var _xObj = _NULL;
  92. for(var i=0; i<ieXmlParsers.length; i++) {
  93. try {
  94. _xObj = new ActiveXObject(ieXmlParsers[i]);
  95. } catch (e) {}
  96. if(_xObj !== _NULL) {
  97. return _xObj;
  98. }
  99. }
  100. return _NULL;
  101. })();
  102. if(xObj === _NULL){
  103. return _NULL;
  104. }
  105. xObj.async = _FALSE;
  106. xObj.loadXML(text);
  107. if (xObj.parseError.errorCode !== 0) {
  108. return _NULL;
  109. }
  110. return new XMLGetty(xObj);
  111. } else {
  112. var oParser = new DOMParser();
  113. var xObj = oParser.parseFromString(new String(text), "text/xml");
  114. return new XMLGetty(xObj);
  115. }
  116. };
  117. /**
  118. * xGetty : Get Node By Xpath
  119. * @example
  120. * var _node = xGetty(node, "/rss/items/title")
  121. *
  122. * @param {element} node - node
  123. * @param {string} path - xpath expression
  124. *
  125. * @return node
  126. * node extends function as xText, xAttr, xGet, xGets
  127. */
  128. _WIN.xGetty = function(node, path) {
  129. if(node === _NULL) {
  130. return _NULL;
  131. }
  132. return new XMLGetty(node.selectSingleNode(path));
  133. };
  134. /**
  135. * xGetties : Get Node List By Xpath
  136. * @example
  137. * var _nodelist = xGetties(node, "/rss/items/title")
  138. *
  139. * @param {element} node - node
  140. * @param {string} path - xpath expression
  141. *
  142. * @return node array
  143. * each node extends function as xText, xAttr, xGet, xGets
  144. */
  145. _WIN.xGetties = function(node, path) {
  146. if(node === _NULL) {
  147. return [];
  148. }
  149. var list = [];
  150. var nodes = node.selectNodes(path);
  151. for(var i=0, len=nodes.length; i<len; i++) {
  152. list.push(new XMLGetty(nodes[i]));
  153. }
  154. return list;
  155. };
  156. })();