Reputation: 17
Hello I am new to the community. I would like to ask a question.
I am trying to create a template HTML5 for loading and doing quizzes. I have the xml file with the question and the answers and I am trying to load it, in my template.
The code I use is this:
To load the xml file
// The Script that loads the XML File Locally only works in Firefox for now
function loadXMLDoc(XMLname) {
var xmlDoc;
if (window.XMLHttpRequest) {
xmlDoc = new window.XMLHttpRequest();
xmlDoc.open("GET", XMLname, false);
xmlDoc.send("");
return xmlDoc.responseXML;
}
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XMLDOM")) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.load(XMLname);
return xmlDoc;
}
else {
xmlhttp = new XMLHttpRequest();
//Open the file using the GET routine
xmlhttp.open("GET", XMLname, false);
//Send request
xmlhttp.send(null);
//xmlDoc holds the document information now
return xmlDoc.responseXML;
}
alert("Error loading document!");
return null;
}
To Pass the Contents to my HTML5 template
xmlDoc=loadXMLDoc("test"+file+".qxml");
My problem is that the data from the xmlfile are not retrieved. While on the server or on any other browser the xmlDoc variable appears as null.
Can you point me in some direction as I am new to the Javascript xmlhttprequest methods. Thanks a lot in advance for your time.
The file extension is not xml (it is .qxml). The problem is the extension of the file .qxml. So it there any way to bypass this and use my extension qxml instead of xml ?
Upvotes: 1
Views: 2432
Reputation: 6408
Try to override the mime type returned by the server, and tell your browser that the data is XML.
// The Script that loads the XML File Locally only works in Firefox for now
function loadXMLDoc(XMLname) {
var xmlDoc;
if (window.XMLHttpRequest) {
xmlDoc = new window.XMLHttpRequest();
xmlDoc.open("GET", XMLname, false);
xmlDoc.overrideMimeType('text/xml');
xmlDoc.send("");
return xmlDoc.responseXML;
}
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XMLDOM")) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.load(XMLname);
return xmlDoc;
}
else {
xmlhttp = new XMLHttpRequest();
//Open the file using the GET routine
xmlhttp.open("GET", XMLname, false);
xmlhttp.overrideMimeType('text/xml');
//Send request
xmlhttp.send(null);
//xmlDoc holds the document information now
return xmlDoc.responseXML;
}
alert("Error loading document!");
return null;
}
Upvotes: 2