/*
@class:		XPath
@author:	david wang 汪翰翔
@version:	2.0 
@link:		http://blog.csdn.net/babydavic
@build:		200904225
@descp:     利用xpath获得xml节点。(兼容了ie,firefox使用)
            1.0 无法在 firefox3.0使用
			2.0 修补这样的问题
@lib:		none
*/
XPath = {
	getIEXmlAX:function (){
		var i,activeXarr;
		var activeXarr = [			
			"MSXML4.DOMDocument",
			"MSXML3.DOMDocument",
			"MSXML2.DOMDocument",
			"MSXML.DOMDocument",
			"Microsoft.XmlDom"
						];	
		for(i=0; i<activeXarr.length; i++){
			try{
				var o = new ActiveXObject(activeXarr[i]);
				return o;
			}catch(e){}
		}
		return false;
	}
	,
	parseXML:function(source){
		try{
			var domParser = new DOMParser();
			var o = domParser.parseFromString(source,'text/xml');	
			return o;
		}catch(e){
			try{
				var o = this.getIEXmlAX();
				o.loadXML(source);
				return o;
			}catch(e){
				return null;
			}
		}
	}
	,
	loadXML:function(path){
		var xmlDoc=null;
		if (window.ActiveXObject){
			xmlDoc=this.getIEXmlAX();
		}else if (document.implementation && document.implementation.createDocument){
			xmlDoc=document.implementation.createDocument("","",null);
		}else{
			alert('Your browser cannot handle this script');
		}
		xmlDoc.async=false;
		xmlDoc.load(path);
		return xmlDoc;
	}
	,
	selectNodes:function(doc,path){
	    var results = [] ; 
	    if( navigator.appName.indexOf('Microsoft')== -1 ){
			var nodesSnapshot = doc.evaluate(path, doc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null );
			for (var i = 0; i < nodesSnapshot.snapshotLength; i++) {
				var thisElement = nodesSnapshot.snapshotItem(i);
				results[i]=thisElement;
			}
		}
		else {
			val = doc.selectNodes(path) ;
			for (var i = 0; i < val.length; i++) {
				results[i] = val(i);
			}
		}
		if(results.length==0){return null ;}
		return results;		
	}
	,
	selectSingleNode:function(doc,path){
	    var result = null ; 
		if( navigator.appName.indexOf('Microsoft')== -1 ){
			var nodesSnapshot = doc.evaluate(path, doc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null );
			for (var i = 0; i < nodesSnapshot.snapshotLength; i++) {
			    var thisElement = nodesSnapshot.snapshotItem(0);
			   	return  thisElement;
			}
		}
		else {
			val = doc.selectNodes(path) ;
			for (var i = 0; i < val.length; i++) {
				return val(0) ;
			}
		}
		return result;
	}
	,
	getNodeValue:function(node){
		if(node!=null)
		return node.firstChild.nodeValue;
	}
};