XMLUtil.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * Created by william on 2016/5/5.
  3. */
  4. //Xml处理类
  5. function XMLUtil(){
  6. this.loadXmlFromFile = function (XmlFilePath){
  7. var _xmlDoc = null;
  8. if(window.ActiveXObject){
  9. _xmlDoc = new ActiveXObject('Microsoft.XMLDOM');//IE
  10. _xmlDoc.async = false;
  11. _xmlDoc.load(XmlFilePath);
  12. }
  13. else if(isFirefox = navigator.userAgent.indexOf("Firefox") > 0){ //fireFox
  14. //_xmlDoc = document.implementation.createDocument('','',null);
  15. //_xmlDoc.load(XmlFilePath);
  16. var xmlHttp = new window.XMLHttpRequest();
  17. xmlHttp.open("GET",XmlFilePath,false);
  18. xmlHttp.send(null);
  19. if(xmlHttp.readyState == 4){
  20. _xmlDoc = xmlHttp.responseXML.documentElement;
  21. }
  22. }
  23. else { //Chorme
  24. var xmlHttp = new window.XMLHttpRequest();
  25. xmlHttp.open("GET",XmlFilePath,false);
  26. xmlHttp.send(null);
  27. if(xmlHttp.readyState == 4){
  28. _xmlDoc = xmlHttp.responseXML.documentElement;
  29. }
  30. }
  31. return _xmlDoc;
  32. }
  33. this.loadXML = function(xmlString){
  34. var _xmlDoc=null;
  35. //判断浏览器的类型
  36. //支持IE浏览器
  37. if(!window.DOMParser && window.ActiveXObject){ //window.DOMParser 判断是否是非ie浏览器
  38. var xmlDomVersions = ['MSXML.2.DOMDocument.6.0','MSXML.2.DOMDocument.3.0','Microsoft.XMLDOM'];
  39. for(var i=0;i<xmlDomVersions.length;i++){
  40. try{
  41. _xmlDoc = new ActiveXObject(xmlDomVersions[i]);
  42. _xmlDoc.async = false;
  43. _xmlDoc.loadXML(xmlString); //loadXML方法载入xml字符串
  44. break;
  45. }catch(e){
  46. }
  47. }
  48. }
  49. //支持Mozilla浏览器
  50. else if(window.DOMParser && document.implementation && document.implementation.createDocument){
  51. try{
  52. /* DOMParser 对象解析 XML 文本并返回一个 XML Document 对象。
  53. * 要使用 DOMParser,使用不带参数的构造函数来实例化它,然后调用其 parseFromString() 方法
  54. * parseFromString(text, contentType) 参数text:要解析的 XML 标记 参数contentType文本的内容类型
  55. * 可能是 "text/xml" 、"application/xml" 或 "application/xhtml+xml" 中的一个。注意,不支持 "text/html"。
  56. */
  57. domParser = new DOMParser();
  58. _xmlDoc = domParser.parseFromString(xmlString, 'text/xml');
  59. }catch(e){
  60. }
  61. }
  62. else{
  63. return null;
  64. }
  65. return _xmlDoc;
  66. }
  67. //将字符串转化成dom对象;string转换为xml**
  68. this.stringToXml = function (xmlString) {
  69. var xmlDoc;
  70. if (typeof xmlString == "string") {
  71. //FF
  72. if (document.implementation.createDocument) {
  73. var parser = new DOMParser();
  74. xmlDoc = parser.parseFromString(xmlString, "text/xml");
  75. } else if (window.ActiveXObject) {
  76. xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  77. xmlDoc.async = false;
  78. xmlDoc.loadXML(xmlString);
  79. }
  80. }
  81. else {
  82. xmlDoc = xmlString;
  83. }
  84. return xmlDoc;
  85. }
  86. //xml转换为string
  87. this.xmlToString = function (xmlDoc) {
  88. if (window.ActiveXObject) {
  89. return xmlDoc.xml; //IE
  90. } else {
  91. return (new XMLSerializer()).serializeToString(xmlDoc); //FF
  92. }
  93. }
  94. this.getNodeTagName = function (node){
  95. var NodeValue,TagName
  96. if(document.all){
  97. NodeValue = node.text;
  98. TagName = node.tagName;
  99. }
  100. else{
  101. NodeValue = node.textContent;
  102. TagName = node.nodeName;
  103. }
  104. return TagName;
  105. }
  106. this.getNodeValue = function (node) {
  107. var value, tagName
  108. if (node.nodeType != 1)
  109. return null;
  110. if (document.all) {
  111. value = node.text; //IE
  112. tagName = node.tagName
  113. }
  114. else {
  115. tagName = node.nodeName;
  116. value = node.textContent;
  117. }
  118. return value;
  119. }
  120. //xml.getElementsByTagName("Result")[0].getAttribute("AllCount")
  121. }