/**
 * 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")
}