/**
 * @author Administrator
 */
//jtree.js
//H:\SourceCodeStudy\web\javascript\JS读取XML例子(兼容IE和FF).rar\JS读取XML例子(兼容IE和FF)\654321
//Comm.$import(Comm.MyBaseUrl+'commonHead/OhsAjax.js','js');
var OHSXml_i=null,OHSXml_j=null;

function OHSXml()
{
	this.isIE=false;
	this.xmlDoc= this.createXMLDoc();

};//

OHSXml.prototype=
{
	createXMLDoc:function()
	{
		var xmldoc;
		if(typeof ActiveXObject != 'undefined')
		{
			//xmldoc = new ActiveXObject("Microsoft.XMLDOM");
			//http://msdn.microsoft.com/en-us/library/ms762314%28v=VS.85%29.aspx
			//目前共有四种MSXML版本，分别为MSXML3、MSXML4、MSXML5和MSXML6。
			//由于在微软的Windows系统中，MSXML主要是用来支持浏览器和Office的，
			//因此这些版本多与不同浏览器版本及Office版本相关。
			//微软推出IE6时带的是MSXML3，因此一般在XP系统上是支持 MSXML3的，MSXML3也成为应用最广泛的一个版本。
			//MSXML5主要用来支持Microsoft Office；而MSXML4很快就被更新版本的MSXML6替代了，
			//微软推荐首选应用最新的MSXML6，其次应用MSXML3。http://blog.sina.com.cn/s/blog_48f93b530100e9tr.html
		    var msxmls=[
			//"MSXML6",
			//"MSXML5",
			//"MSXML4",
			"MSXML3",
			"MSXML2.6",			
			"MSXML2",
			"Microsoft",
			"MSXML"
			];
			
			var msxmlver = [
			".DOMDocument.6.0",
			".DOMDocument.4.0",
			".DOMDocument.3.0",			
			".DOMDocument.2.6",					
			".DOMDocument"	
			];
			
			if(OHSXml_i==null && OHSXml_j==null)
			{
			    for(var i=0;i<msxmls.length;i++)
				{
					for(var j=0;j<msxmlver.length;j++)
					{
					   try
					   {
						xmldoc =  new ActiveXObject(msxmls[i]+msxmlver[j])
					   }catch(e)
					   {
					   	    
							continue;
					   }
					   //alert(i+','+j);
					   break;					
					}//for2
					
					if (xmldoc) {
						OHSXml_i = i;
						OHSXml_j = j;
						break;
					}
				}				
			}//if(OHSXml_i==null && OHSXml_j==null)
			else
			{
				xmldoc =  new ActiveXObject(msxmls[OHSXml_i]+msxmlver[OHSXml_j]);
			}
			

			
			//若没有指定SelectionLanguage属性值为XPath则要注意以下情况：
			//   1. 数组下标从0开始（我们知道在XPath查询表达式中数组下标是从1开始的）
			//   2. 不支持在XPath查询表达式中使用XPath函数。
			xmldoc.setProperty("SelectionLanguage","XPath"); 
			//MSXML2.6以后的版本都支持，我发现!
			if(OHSXml_i<=1)
			{
			    xmldoc.setProperty("NewParser", true);//This property is supported in MSXML 4.0 and later. The default value is false.	
			}
			

			xmldoc.validateOnParse = false;
			 
    		//throw new Error("Could not find an installed XML parser!");
  
			/*
		     var activeX = ['MSXML2.XMLHTTP.6.0',
		     'Msxml2.XMLHTTP.5.0',
		     'Msxml2.XMLHTTP.4.0',
		     'Msxml2.XMLHTTP.3.0',
		     'Msxml2.XMLHTTP.2.6',
		     'Msxml2.XMLHTTP.1.0',
		     'Microsoft.XMLHTTP.1',
		     'Microsoft.XMLHTTP',
		     'MSXML2.XMLHTTP'];
		     for (var i = 0; i < activeX.length; ++i) 
			 {
			 	try 
				{
			 		xmldoc = new ActiveXObject(activeX[i]);
					alert(activeX[i]);
					;
			 		break;
			 	} 
			 	catch (e) 
				{
			 	}
			 }//for
			 */

			this.isIE=true;
		}else if(document.implementation && document.implementation.createDocument)
		{
 
			xmldoc= document.implementation.createDocument("","doc",null);
		}
		
		xmldoc.async = true;
		//为了和FireFox一至，这里不能改为False;
		xmldoc.preserveWhiteSpace = true;
		return xmldoc;
	},
	Load:function(xmlFile)
	{
	    if (!this.xmlDoc) 
	    {
		    this.xmlDoc = this.createXMLDoc();
	    }

		try
		{
			this.xmlDoc.load(xmlFile);
		}catch(e)
		{
			var  ajax = new OHSAjax(xmlFile);
			ajax.method = "GET";
			ajax.runAJAX();
			this.xmlDoc =  ajax.responseXML;
			ajax =null;
		}
		return this.xmlDoc;
	},
	loadXML:function(xmlstr)
	{
        if(!xmlstr)
        {
            return null;
        }
 
	    if (!this.xmlDoc) 
	    {
		    this.xmlDoc = this.createXMLDoc();
	    }
	    
		if (this.xmlDoc == null) 
		{
			alert('您的浏览器不支持xml文件读取,于是本页面禁止您的操作,推荐使用IE5.0以上可以解决此问题!');
			return null;
		}
		
		if (typeof xmlstr != 'string') 
		{
		    xmlstr = xmlstr.value;
		}
		
		if(this.isIE)
		{
				try
				{
					//this.xmlDoc.setProperty("NewParser", 'true');
					//this.xmlDoc.validateOnParse = true;

					this.xmlDoc.loadXML(xmlstr);
				}catch(e)
				{
					alert('LoadXML occur exception--'+e.message);
				}		
		}else
		{
			var parser = new DOMParser();
			
			try
			{
			    this.xmlDoc = parser.parseFromString(xmlstr,"text/xml");
			}catch(e)
			{
				;//alert('LoadXML occur exception--'+e.message);
			}
				

		}

		return this.xmlDoc;
	},
	LoadXML2:function(xmlString)
	{
	    var childNodes = this.childNodes;
	    for (var i = childNodes.length - 1; i >= 0; i--)
	        this.removeChild(childNodes[i]);
	
	    var dp = new DOMParser();
	    var newDOM = dp.parseFromString(xmlString, "text/xml");
	    var newElt = this.importNode(newDOM.documentElement, true);
	    this.appendChild(newElt);
	},
	selectNodes:function (xPath)
    {
        if(window.ActiveXObject)
        { 
			//var iterator = document.evaluate(xPath,this.xmlDoc,nsResolver,nodeSet, null ); 
            return this.xmlDoc.documentElement.selectNodes(xPath);
        }
        else
        {
           var xpe = new XPathEvaluator();
		   var nodeSet = XPathResult.ORDERED_NODE_ITERATOR_TYPE;//
           var nsResolver = xpe.createNSResolver( this.xmlDoc.ownerDocument == null ? this.xmlDoc.documentElement : this.xmlDoc.ownerDocument.documentElement);
           return xpe.evaluate(xPath,this.xmlDoc,nsResolver,nodeSet, null);
		   //https://developer.mozilla.org/en/Introduction_to_using_XPath_in_JavaScript
        }
    },
	selectSingleNode:function ( elementPath)
    {
        if(window.ActiveXObject)
        {
            return this.xmlDoc.selectSingleNode(elementPath);
        }
        else
        {
           var xpe = new XPathEvaluator();
		   var nodeSet = XPathResult.FIRST_ORDERED_NODE_TYPE//XPathResult.ORDERED_NODE_ITERATOR_TYPE;//
           var nsResolver = xpe.createNSResolver( this.xmlDoc.ownerDocument == null ? this.xmlDoc.documentElement : this.xmlDoc.ownerDocument.documentElement);
           var results = xpe.evaluate(elementPath,this.xmlDoc,nsResolver,nodeSet, null);
           return results.singleNodeValue; 
		   //return results.iterateNext();
        }
    },
 getChildren:function(node,name){  
     var nodes = [];  
     for(var i=0;i<node.childNodes.length;i++){  
         if(node.childNodes[i].nodeName == name){  
             nodes[nodes.length] = node.childNodes[i];  
         }  
     }  
     return nodes;  
 }  
 ,  
 getChild:function(node,name){         
     for(var i=0;i<node.childNodes.length;i++){  
         if(node.childNodes[i].nodeName == name){  
             return node.childNodes[i];  
         }  
     }  
     return null;  
 }  
 ,  
 //http://www.klstudio.com/post/156.html
 selectNodes2:function(node,path,child){  
     var paths = path.split("/");  
     for(var i=0;i<paths.length;i++){  
         node = this.getChild(node,paths[i]);  
         if(node == null){  
             return [];  
         }  
     }  
     return this.getChildren(node,child);          
 }  
 ,  
 selectSingleNode2:function(node,path){  
     var paths = path.split("/");  
     for(var i=0;i<paths.length;i++){  
         node = this.getChild(node,paths[i]);  
         if(node == null){  
             return null;  
         }  
     }         
     return node;  
 }, 
	//取出指定节点的属性。H:\SourceCodeStudy\web\javascript\JTree2.0\JTree\includes
	getDOMAtt: function(pNode,pAttr)
	{
		try{
			return pNode.attributes.getNamedItem(pAttr).nodeValue;
		}catch(e)
		{
			return false;
		}
	},
	GetNodes:function(xPath)
	{
		var node;
		//;
		node = this.SelectNodes( xPath);
//		if(this.isIE)
//		{
//			node = this.xmlDoc.selectSingleNode(xPath);
//		}else
//		{
//			node = this.xmlDoc.getElementsByTagName(xPath);//.childNodes;
//		}
		//
		return node;
	},
	GetNodeName:function(xPath,n)
	{
		var node= this.GetNodes(xPath);
		var  name;
		name= node[n].getAttribute('Text');
		return name;
	},
	GetNodeValue:function(xPath)
	{
		var node= this.GetNodes(xPath);
		var  value;
		//value= node(0).getAttribute('Value');
		
		if(this.isIE)
		{
			value= node.childNodes[0].text;
		}else
		{
			//var values = this.xmlDoc.getElementsByTagName("_id")[0].textContent.split(' ');//
			//var values= node.childNodes[1].textContent.split(' ');
			value = obj.firstChild.nodeValue;//node.childNodes[0].textContent;//
			//value= values[0];			
		}
		return value;
	},
	Traverse:function(xmltree) 
	{
		var str ='';
	    if(xmltree.hasChildNodes()) 
		{
	        str ='<ul><li>';
	        str +='<b>'+xmltree.tagName+' : </b>';
	        var nodes=xmltree.childNodes.length;
	        for(var i=0; i<xmltree.childNodes.length; i++)
	            str +=this.Traverse(xmltree.childNodes[i]);
	        str +='</li></ul>';
	    }
	    else
		{
			str +=xmltree.text;
		}
        return str;
	}
	
};
