var XMLHandler = function () { this.DOMDocument = null; } /** * Return a XMLHttpRequest Handler. * Currently works for both IE and Gecko. * Maybe add support for Opera? * * @see http://www.scss.com.au/scripts/xmlhttprequest.js */ XMLHandler.prototype.GetHttpRequest = function () { if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else if (window.ActiveXObject) { var aMSXML = new Array( 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'); for (var i = 0; i < aMSXML.length; i++) { try { return new ActiveXObject(aMSXML[i]); } catch (e) {} } return null; } } /** * Load a specific URL into this XMLHandler. * If asyncFunction is specified, this function will return immediately * and the asyncFunction will be called when the document has been * fetched. */ XMLHandler.prototype.LoadURL = function (xmlUrl, asyncFunction, silentRequest) { var bAsync = (typeof(asyncFunction) == 'function'); var oXmlHttp = this.GetHttpRequest(); oXmlHttp.open('GET', xmlUrl, bAsync); if (bAsync) { var oXmlHandler = this; oXmlHttp.onreadystatechange = function () { if (oXmlHttp.readyState == 4) { if (oXmlHttp.status == 200) { if (oXmlHttp.responseXML != null) { oXmlHandler.DOMDocument = oXmlHttp.responseXML; asyncFunction(oXmlHandler); } else { if (!silentRequest && confirm('XML Response Error.\nView response?')) { alert('-= REQUEST =-\n' + xmlUrl + '\n\n-= RESPONSE =-\n' + oXmlHttp.responseText); } } } else if (!silentRequest) { alert('XML Request Error: (' + oXmlHttp.status + ') ' + oXmlHttp.statusText); } } } } try { oXmlHttp.send(null); if (!bAsync) { if (oXmlHttp.status == 200) { if (oXmlHttp.responseXML != null) { this.DOMDocument = oXmlHttp.responseXML; } else { if (!silentRequest && confirm('XML Response Error.\nView response?')) { alert('-= REQUEST =-\n' + xmlUrl + '\n\n-= RESPONSE =-\n' + oXmlHttp.responseText); } return false; } } else { if (!silentRequest) { alert('XML Request Error: (' + oXmlHttp.status + ') ' + oXmlHttp.statusText); } return false; } } } catch (e) { if (!silentRequest) { alert('XML Request Error: Unable to connect.'); } return false; } return true; } /** * Select nodes in the fetched XML document according to the specified * xpath. The nodes are returned as an array. */ XMLHandler.prototype.SelectNodes = function (xpath) { try { return this.DOMDocument.selectNodes(xpath); } catch (e) {} var aNodes = new Array(); var xPathResult = this.DOMDocument.evaluate(xpath, this.DOMDocument, this.DOMDocument.createNSResolver(this.DOMDocument.documentElement), XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); if (xPathResult) { var oNode; while (oNode = xPathResult.iterateNext()) { aNodes[aNodes.length] = oNode; } } return aNodes; } /** * Select the first node in the XML document according to the specified * xpath. */ XMLHandler.prototype.SelectNode = function (xpath) { try { return this.DOMDocument.selectSingleNode(xpath); } catch (e) {} var xPathResult = this.DOMDocument.evaluate(xpath, this.DOMDocument, this.DOMDocument.createNSResolver(this.DOMDocument.documentElement), XPathResult.FIRST_ORDERED_NODE_TYPE, null); if (xPathResult && xPathResult.singleNodeValue) { return xPathResult.singleNodeValue; } return null; }