source: [view]
var _document = dojo.doc;
var doc;
mimetype = mimetype || "text/xml";
if(str && dojo.trim(str) && "DOMParser" in dojo.global){
//Handle parsing the text on Mozilla based browsers etc..
var parser = new DOMParser();
doc = parser.parseFromString(str, mimetype);
var de = doc.documentElement;
var errorNS = "http://www.mozilla.org/newlayout/xml/parsererror.xml";
if(de.nodeName == "parsererror" && de.namespaceURI == errorNS){
var sourceText = de.getElementsByTagNameNS(errorNS, 'sourcetext')[0];
if(sourceText){
sourceText = sourceText.firstChild.data;
}
throw new Error("Error parsing text " + de.firstChild.data + " \n" + sourceText);
}
return doc;
}else if("ActiveXObject" in dojo.global){
//Handle IE.
var ms = function(n){ return "MSXML" + n + ".DOMDocument"; };
var dp = ["Microsoft.XMLDOM", ms(6), ms(4), ms(3), ms(2)];
dojo.some(dp, function(p){
try{
doc = new ActiveXObject(p);
}catch(e){ return false; }
return true;
});
if(str && doc){
doc.async = false;
doc.loadXML(str);
var pe = doc.parseError;
if(pe.errorCode !== 0){
throw new Error("Line: " + pe.line + "\n" +
"Col: " + pe.linepos + "\n" +
"Reason: " + pe.reason + "\n" +
"Error Code: " + pe.errorCode + "\n" +
"Source: " + pe.srcText);
}
}
if(doc){
return doc; //DOMDocument
}
}else if(_document.implementation && _document.implementation.createDocument){
if(str && dojo.trim(str) && _document.createElement){
//Everyone else that we couldn't get to work. Fallback case.
// FIXME: this may change all tags to uppercase!
var tmp = _document.createElement("xml");
tmp.innerHTML = str;
var xmlDoc = _document.implementation.createDocument("foo", "", null);
dojo.forEach(tmp.childNodes, function(child){
xmlDoc.importNode(child, true);
});
return xmlDoc; // DOMDocument
}else{
return _document.implementation.createDocument("", "", null); // DOMDocument
}
}
return null; // null