﻿var DomParseXml = function ()
{
    
    //var itr = 0;
}

//定义
DomParseXml.prototype.getXMLDocument=function() 
{
	var xDoc = null;
	
	if (document.implementation && document.implementation.createDocument) {
		xDoc = document.implementation.createDocument("","",null);
	}
	else
	{
		if ((typeof ActiveXObject) != "undefined") 
		{
			try {
				xDoc = new ActiveXObject("Msxml2.DOMDocument");
			}
			catch (e) 
			{
				xDoc = new ActiveXObject("Msxml.DOMDocument");
			}
		}
	}
	
	if (xDoc == null) 
	{
		xDoc = null;
	}

	return xDoc;
}

DomParseXml.prototype.getValue = function(sPath,DocXml)
{
	var sReturn = null;
	var isIE = window.Event?false:true;
	var bolLoad;
	if (DocXml)
	{
		bolLoad = true;
	}	

	if(bolLoad)
	{
		sPath = this.repairInput(sPath);
	
		var objNode = this.getNode(sPath,DocXml);
		
		if (isIE)
		{
			objNode = objNode[0].childNodes[0];
			
			if(objNode != null)
			{
				sReturn = objNode.text;
			}
		}
		else
		{
			if(objNode)
			{
				for(var z=0;z<objNode[0].childNodes.length;z++)
				{
					if(objNode[0].childNodes[z].nodeValue != "" && objNode[0].childNodes[z].nodeValue != null)
					{
						if(sReturn == null)
						{
							sReturn = objNode[0].childNodes[z].nodeValue;
						}
						else
						{
							sReturn = sReturn + objNode[0].childNodes[z].nodeValue;
						}
					}
				}
			}
		}	
	}
	else
	{
		alert("Data is not available");
	}
	
	DocXml = null;
	return(sReturn);
}

DomParseXml.prototype.getValueArray = function(sPath,DocXml)
{
	var ArrReturn = new Array();
	var iArrReturnLen = 0;
	var isIE = window.Event?false:true;
	var bolLoad;
	if (DocXml)
	{
		bolLoad = true;
	}	
			
	if(bolLoad)
	{
		sPath = this.repairInput(sPath);
		
		var objNode = this.getNode(sPath,DocXml);
		
		if (isIE)
		{
			if(objNode != null)
			{
				for(var i=0;i<objNode[0].childNodes.length;i++)
				{
					var cNode = objNode[0].childNodes(i);	
					ArrReturn[iArrReturnLen] = cNode.text;
					iArrReturnLen++;
				}
			}
		}
		else
		{
			for (var i=0;i<Node.childNodes.length;i++)
			{
				ArrReturn[iArrReturnLen] = objNode.childNodes(i).text;	
				iArrReturnLen++;
			}
		}
	}
	else
	{
		alert("Data is not available");
	}
	
	DocXml = null;
	return(ArrReturn);
}

DomParseXml.prototype.getNodeslength = function(DocXml,sPath)
{
	var Return = "";
	var isIE = window.Event?false:true;
	if (isIE)
	{
		var Node = DocXml.selectSingleNode(sPath);
		Return = Node.childNodes.length;
	}
	else
	{
		sPath = "Contents/"+sPath;
		var oNSResolver = DocXml.createNSResolver(DocXml.documentElement);
		var aItems = DocXml.evaluate(sPath, DocXml, oNSResolver,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		
		Return = aItems.snapshotLength;
	}
	return Return;
}

DomParseXml.prototype.getNodeAttrib = function(sPath,DocXml,attribeName)
{
	var isIE = window.Event?false:true;
	var sReturn = "";
	var bolLoad;
	if (DocXml)
	{
		bolLoad = true;
	}	
	
	if(bolLoad)
	{
		sPath = this.repairInput(sPath);
		var Node = this.getNode(sPath,DocXml);

		if(Node[0] != null)
		{
			Node = Node[0].attributes;
			
			if (Node != null)
			{
				var i=0;
				
				while(Node[i] != null)
				{
					if(Node[i].name == attribeName)
					{
						if (!isIE)
						{
							sReturn = Node[i].nodeValue;
						}
						else
						{
							sReturn = Node[i].text;
						}
						break;
					}
					i++;
				}
			}
			if(sReturn == null)
			{
				alert("attribute is now available");
			}
		}
	}
	else
	{
		alert("Data is not available");
	}
	DocXml = null;
	
	return sReturn;
}

DomParseXml.prototype.getNodeAttribToIndex = function(sPath,DocXml,attribeName,i)
{
	var isIE = window.Event?false:true;
	var sReturn = null;
	if (DocXml)
	{
		bolLoad = true;
	}	
	
	if(bolLoad)
	{
		sPath = this.repairInput(sPath);
		var Node = this.getNode(sPath,DocXml);
		
		Node = Node[i].attributes;
	
		if (Node != null)
		{
			var i=0;
			while(Node[i] != null)
			{
				if(Node[i].name == attribeName)
				{
					if (!isIE)
					{
						sReturn = Node[i].nodeValue;
					}
					else
					{
						sReturn = Node[i].text;
					}
				}
				i++;
			}
		}
		
		if(sReturn == null)
		{
			sReturn = "";
		}
	}
	else
	{
		alert("Data is not available");
	}
	DocXml = null;
	return sReturn;
}

DomParseXml.prototype.getNodeAttribArray = function(sPath,DocXml,attribeName)
{
	var ArrReturn = new Array();
	var iArrReturnLen = 0;
	var isIE = window.Event?false:true;
	if (DocXml)
	{
		bolLoad = true;
	}
	if(bolLoad)
	{
		sPath = this.repairInput(sPath);
		var Node = this.getNode(sPath,DocXml); 
		
		if (isIE)
		{
			Node = Node[0];
		}
		else
		{
			Node = Node[0];
		}	

		if(Node)
		{
			for(var i=0;i<Node.childNodes.length;i++)
			{
				var oNode = Node.childNodes[i].attributes;	
				
				var x=0;
				while(oNode[x] != null)
				{
					if(oNode[x].name == attribeName)
					{
						if (!isIE)
						{
							if(oNode[x].nodeValue == null || oNode[x].nodeValue == "")
							{
								ArrReturn[iArrReturnLen] = "";
							}
							else
							{
								ArrReturn[iArrReturnLen] = oNode[x].nodeValue;
							}	
						}
						else
						{
							if(oNode[x].text == null || oNode[x].text == "")
							{
								ArrReturn[iArrReturnLen] = "";
							}
							else
							{
								ArrReturn[iArrReturnLen] = oNode[x].text;
							}	
						}
						
						iArrReturnLen++;
					}
					x++;
				}
			}
		}
	}
	else
	{
		alert("Data is not available");
	}
	DocXml = null;
	return ArrReturn;
}


DomParseXml.prototype.getNodeAttribArrayToIndex = function(sPath,DocXml,attribeName,iLen)
{
	var ArrReturn = new Array();
	var iArrReturnLen = 0;
	var bolLoad = false;
	if (DocXml)
	{
		bolLoad = true;
	}	
	var isIE = window.Event?false:true;
	if(bolLoad)
	{
		sPath = this.repairInput(sPath);
		
		var Node = this.getNode(sPath,DocXml); 
		
		if (isIE)
		{
			Node = Node[iLen];
		}
		else
		{
			Node = Node[0];
		}	

		for(var i=0;i<Node.childNodes.length;i++)
		{
			var oNode = Node.childNodes(i).attributes;	
			var x=0;
			while(oNode[x] != null)
			{
				if(oNode[x].name == attribeName)
				{
					if (!isIE)
					{
						if(oNode[x].nodeValue == null || oNode[x].nodeValue == "")
						{
							ArrReturn[iArrReturnLen] = "";
						}
						else
						{
							ArrReturn[iArrReturnLen] = oNode[x].nodeValue;
						}	
					}
					else
					{
						if(oNode[x].text == null || oNode[x].text == "")
						{
							ArrReturn[iArrReturnLen] = "";
						}
						else
						{
							ArrReturn[iArrReturnLen] = oNode[x].text;
						}	
					}
					iArrReturnLen++;
				}
				x++;
			}
		}
	}
	else
	{
		alert("Data is not available");
	}
	DocXml = null;
	return ArrReturn;
}

DomParseXml.prototype.getNode = function(sPath,DocXml)
{  
	var isIE = window.Event?false:true;
	if (isIE)
	{
		DocXml = DocXml.documentElement;
		return DocXml.selectNodes(sPath);
	}
	else
	{
		sPath = "Contents/"+sPath;
		var oNSResolver = DocXml.createNSResolver(DocXml.documentElement);
		var aItems = DocXml.evaluate(sPath, DocXml, oNSResolver,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		var aResult = [];
				
		for( var i = 0; i < aItems.snapshotLength; i++)
		{
			aResult[i] = aItems.snapshotItem(i);
		}
		
		if (aResult.length > 0)
		{
			if (aResult[0].childNodes.length == 0)
			{
				return(aResult);
			}
			else
			{
				return(aResult);
			}
		}
		else
		{
			return("");
		}
	} 
}


DomParseXml.prototype.repairInput = function(sPath)
{
	var iLen = sPath.length-1;
	if (sPath.lastIndexOf("/") == iLen)
	{
		sPath = sPath.substr(0,sPath.length-1);
	}
	if (sPath.indexOf("/") == 0)
	{
		sPath = sPath.substr(1,sPath.length-1);
	}
	
	return(sPath);
}

DomParseXml.prototype.Write = function()
{
	var DocXml = this.getXMLDocument();
	var p = doc.createProcessingInstruction("xml","version='1.0' encoding='unicode'"); 

    //添加文件头 
    doc.appendChild(p); 
}

DomParseXml.prototype.onlyValue = function(sData)
{
	var sReturn = this.getValue("Result",sData);
	return(sReturn); 
}



