
// 
// XmlParser.js
// 
// A JavaScript object that can retrieve XML files from a server via AJAX and/or
// parse XML strings into a collection of XmlNode objects.
// 
// 
// Copyright (C) 2003-2005 Kody Brown. All Rights Reserved.
// 
// This is my own personal project maintained by me and is not owned nor supported by my employer, 
// my previous employer(s), or anyone else. This code and/or application is presented as is, without 
// expressed or implied warranty of any kind. Use it at your own risk. Period. By downloading and/or 
// using any file or information from this web-site (http://www.wasatchwizard.com/) you agree to be 
// held to these terms.
// 
// You are given rights to use this code/application for personal and/or professional use provided 
// that the copyright and this notice remains included and unchanged.
// 
// 


function XmlParser(url_or_string)
{
// member variables
	this.rootElement = new XmlNode();

// member functions
	this.Load = XmlParser_Load;
	this.LoadXml = XmlParser_LoadXml;
	this.LoadFile = XmlParser_LoadFile;
	
// initialization
	this.Load(url_or_string);
	
	return this;
}

function XmlParser_Load(url_or_string)
{
	if (null != url_or_string && undefined != url_or_string && url_or_string.length > 0)
	{
		if (url_or_string[0] = "<")
		{
			this.rootElement = this.LoadXml(url_or_string);
		}
		else
		{
			this.rootElement = this.LoadFile(url_or_string);
		}
	}
}

function XmlParser_LoadFile(url)
{
	var xmlReq = new Object();
	
	if (window.XMLHttpRequest)
	{
		// probably Mozilla
		try { xmlReq = new XMLHttpRequest(); }
		catch(e) { }
	}
	else if (window.ActiveXObject)
	{
		// probably IE
		try { xmlReq = new ActiveXObject("Microsoft.XMLHTTP"); }
		catch(e)
		{
			try { xmlDoc = new ActiveXObject("Msxml2.DOMDocument"); }
			catch(e)
			{
				try { xmlDoc = new ActiveXObject("Microsoft.DOMDocument"); }
				catch(e) { }
			}
		}
	}
	
	if (xmlReq)
	{
		xmlReq.open("GET", url, false);
		xmlReq.send(null);
		
		if (xmlReq.status == 200)
		{
			return this.LoadXml(xmlReq.responseText);
		}
		else
		{
			// an error occurred... ?
		}
	}
	
	return false;
}

function XmlParser_LoadXml(xml)
{
	xml = trimAll(xml);
	if (xml.substring(0, 5) == "<?xml")
	{
		this.rootElement = new XmlNode(xml);
		return (null != this.rootElement) ? true : false;
	}
	else
	{
		alert("XML is not well-formed");
		this.rootElement = null;
		return false;
	}
}


