123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- /**
- * Created by william on 2016/5/5.
- */
- //Xml处理类
- function XMLUtil(){
- this.loadXmlFromFile = function (XmlFilePath){
- var _xmlDoc = null;
- if(window.ActiveXObject){
- _xmlDoc = new ActiveXObject('Microsoft.XMLDOM');//IE
- _xmlDoc.async = false;
- _xmlDoc.load(XmlFilePath);
- }
- else if(isFirefox = navigator.userAgent.indexOf("Firefox") > 0){ //fireFox
- var xmlHttp = new window.XMLHttpRequest();
- xmlHttp.open("GET",XmlFilePath,false);
- xmlHttp.send(null);
- if(xmlHttp.readyState == 4){
- _xmlDoc = xmlHttp.responseXML.documentElement;
- }
- // _xmlDoc = document.implementation.createDocument('','',null);
- // _xmlDoc.load(XmlFilePath);
- }
- else { //Chorme
- var xmlHttp = new window.XMLHttpRequest();
- xmlHttp.open("GET",XmlFilePath,false);
- xmlHttp.send(null);
- if(xmlHttp.readyState == 4){
- _xmlDoc = xmlHttp.responseXML.documentElement;
- }
- }
- return _xmlDoc;
- }
- this.loadXML = function(xmlString){
- var _xmlDoc=null;
- //判断浏览器的类型
- //支持IE浏览器
- if(!window.DOMParser && window.ActiveXObject){ //window.DOMParser 判断是否是非ie浏览器
- var xmlDomVersions = ['MSXML.2.DOMDocument.6.0','MSXML.2.DOMDocument.3.0','Microsoft.XMLDOM'];
- for(var i=0;i<xmlDomVersions.length;i++){
- try{
- _xmlDoc = new ActiveXObject(xmlDomVersions[i]);
- _xmlDoc.async = false;
- _xmlDoc.loadXML(xmlString); //loadXML方法载入xml字符串
- break;
- }catch(e){
- }
- }
- }
- //支持Mozilla浏览器
- else if(window.DOMParser && document.implementation && document.implementation.createDocument){
- try{
- /* DOMParser 对象解析 XML 文本并返回一个 XML Document 对象。
- * 要使用 DOMParser,使用不带参数的构造函数来实例化它,然后调用其 parseFromString() 方法
- * parseFromString(text, contentType) 参数text:要解析的 XML 标记 参数contentType文本的内容类型
- * 可能是 "text/xml" 、"application/xml" 或 "application/xhtml+xml" 中的一个。注意,不支持 "text/html"。
- */
- domParser = new DOMParser();
- _xmlDoc = domParser.parseFromString(xmlString, 'text/xml');
- }catch(e){
- }
- }
- else{
- return null;
- }
- return _xmlDoc;
- }
- //将字符串转化成dom对象;string转换为xml**
- this.stringToXml = function (xmlString) {
- var xmlDoc;
- if (typeof xmlString == "string") {
- //FF
- if (document.implementation.createDocument) {
- var parser = new DOMParser();
- xmlDoc = parser.parseFromString(xmlString, "text/xml");
- } else if (window.ActiveXObject) {
- xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
- xmlDoc.async = false;
- xmlDoc.loadXML(xmlString);
- }
- }
- else {
- xmlDoc = xmlString;
- }
- return xmlDoc;
- }
- //xml转换为string
- this.xmlToString = function (xmlDoc) {
- if (window.ActiveXObject) {
- return xmlDoc.xml; //IE
- } else {
- return (new XMLSerializer()).serializeToString(xmlDoc); //FF
- }
- }
- this.getNodeTagName = function (node){
- var NodeValue,TagName
- if(document.all){
- NodeValue = node.text;
- TagName = node.tagName;
- }
- else{
- NodeValue = node.textContent;
- TagName = node.nodeName;
- }
- return TagName;
- }
- this.getNodeValue = function (node) {
- var value, tagName
- if (node.nodeType != 1)
- return null;
- if (document.all) {
- value = node.text; //IE
- tagName = node.tagName
- }
- else {
- tagName = node.nodeName;
- value = node.textContent;
- }
- return value;
- }
- //xml.getElementsByTagName("Result")[0].getAttribute("AllCount")
- }
|