// Obtaining XML Documents by <<JavaScript: The Definitive Guide, 5th Edition>>
XML={};
XML.newDocument = function(rootTagName, namespaceURL) {
    if (!rootTagName) rootTagName = "";
    if (!namespaceURL) namespaceURL = "";
    if (document.implementation && document.implementation.createDocument) {
        // This is the W3C standard way to do it
        return document.implementation.createDocument(namespaceURL,rootTagName, null);
    }else { // This is the IE way to do it
        var doc = new ActiveXObject("MSXML2.DOMDocument");
        if (rootTagName) {
            var prefix = "";
            var tagname = rootTagName;
            var p = rootTagName.indexOf(':');
            if (p != -1) {
                prefix = rootTagName.substring(0, p);
                tagname = rootTagName.substring(p+1);
            }
            if (namespaceURL) {
                if (!prefix) prefix = "a0"; // What Firefox uses
            }
            else prefix = "";
            var text = "<" + (prefix?(prefix+":"):"") +  tagname +(namespaceURL?(" xmlns:" + prefix + '="' + namespaceURL +'"'):"") +"/>";
            doc.loadXML(text);
        }
        return doc;
    }
};
/**
 * Synchronously load the XML document at the specified URL and
 * return it as a Document object
 */
XML.load = function(url) {
    var xmldoc = XML.newDocument( );
    xmldoc.async = false;
    xmldoc.load(url);
    return xmldoc;
};
/**
 * Asynchronously load and parse an XML document from the specified URL.
 * When the document is ready, pass it to the specified callback function.
 * This function returns immediately with no return value.
 */
XML.loadAsync = function(url, callback,arg1,arg2,arg3) {
    var xmldoc = XML.newDocument( );
    if (document.implementation && document.implementation.createDocument) {
        xmldoc.onload = function( ) { callback(xmldoc,arg1,arg2,arg3); };
    }else {
        xmldoc.onreadystatechange = function( ) {
            if (xmldoc.readyState == 4) callback(xmldoc,arg1,arg2,arg3);
        };
    }
    xmldoc.load(url);
};
/**
 * Parse the XML document contained in the string argument and return
 * a Document object that represents it.
 */
XML.parse = function(text) {
    if (typeof DOMParser != "undefined") {
        // Mozilla, Firefox, and related browsers
        return (new DOMParser( )).parseFromString(text, "application/xml");
    } else if (typeof ActiveXObject != "undefined") {
        // Internet Explorer.
        var doc = XML.newDocument( );  // Create an empty document
        doc.loadXML(text);            // Parse text into it
        return doc;                   // Return it
    }else {
        // As a last resort, try loading the document from a data: URL
        // This is supposed to work in Safari. Thanks to Manos Batsis and
        // his Sarissa library (sarissa.sourceforge.net) for this technique.
        var url = "data:text/xml;charset=utf-8," + encodeURIComponent(text);
        var request = new XMLHttpRequest( );
        request.open("GET", url, false);
        request.send(null);
        return request.responseXML;
    }
};