Intermittent Internet Explorer Error whilst using Javascript to traverse XML Nodes
Posted
by Sykth
on Stack Overflow
See other posts from Stack Overflow
or by Sykth
Published on 2010-05-14T12:44:57Z
Indexed on
2010/05/17
8:10 UTC
Read the original article
Hit count: 369
Hi there all,
Basically I use a javascript SOAP plugin to send and receive XML from a web service.
Recently I've been experiencing intermittment (1 in every 20-30 times) errors in IE when trying to access data in the XML file.
Bear with me on this, I'm going to try and go into a fair amount of detail to help anyone who is willing to read this:
I have a html file, with an external javascript file attached.
In the javascript file I include 5 global variables:
var soapRes;
var questionXML;
var xRoot;
var assessnode;
var edStage = 0;
Once the html file is at a readystate I request the XML file from the web service like this:
function initNextStage(strA) {
// Set pl Parameters here
var pl = new SOAPClientParameters();
// soap call
var r = SOAPClient.invoke(url, "LoadAssessment", pl, true, initStage_callBack);
}
function initStage_callBack(r, soapResponse)
{
soapRes = soapResponse;
initvalues();
function initvalues(){
xRoot = soapRes.documentElement;
assessnode = xRoot.getElementsByTagName("ed")[0];
questionXML = assessnode.getElementsByTagName("question")[edStage];
}
}
Once this is completed the XML should be loaded into memory via the global variable soapRes.
I then save values to the XML before moving one node down the XML tree, and pulling the next set of values out of it.
function submitAns1() {
var objAns;
var corElem;
var corElemTxt;
questionXML = assessnode.getElementsByTagName("question")[edStage];
objAns = questionXML.getElementsByTagName("item")[0].getAttribute("answer");
corElem = soapRes.createElement("correct");
corElemTxt = soapRes.createTextNode("value");
questionXML.appendChild(corElem);
corElem.appendChild(corElemTxt);
if(objAns == "t") {
corElemTxt.nodeValue = "true";
}
else {
corElemTxt.nodeValue = "false";
}
edStage++;
questionXML = assessnode.getElementsByTagName("question")[edStage];
var inc1 = questionXML.getElementsByTagName("sentence")[0];
var inc2 = questionXML.getElementsByTagName("sentence")[1];
var edopt1 = questionXML.getElementsByTagName("item")[0];
var edopt2 = questionXML.getElementsByTagName("item")[1];
var edopt3 = questionXML.getElementsByTagName("item")[2];
var edopt4 = questionXML.getElementsByTagName("item")[3];
var edopt5 = questionXML.getElementsByTagName("item")[4];
var temp1 = edopt1.childNodes[0].nodeValue;
var temp2 = edopt2.childNodes[0].nodeValue;
var temp3 = edopt3.childNodes[0].nodeValue;
var temp4 = edopt4.childNodes[0].nodeValue;
var temp5 = edopt5.childNodes[0].nodeValue;
}
This is an example of our XML:
<ed>
<appid>Administrator</appid>
<formid>ED009</formid>
<question id="1">
<sentence id="1">[email protected]</sentence>
<sentence id="2">[email protected]</sentence>
<item id="0" answer="f" value="0">0</item>
<item id="1" answer="f" value="1">1</item>
<item id="2" answer="f" value="2">2</item>
<item id="3" answer="t" value="3">3</item>
<item id="4" answer="f" value="4">4</item>
</question>
<question id="2">
<sentence id="1">Beausdene 13</sentence>
<sentence id="2">Beauscene 83</sentence>
<item id="0" answer="f" value="0">0</item>
<item id="1" answer="f" value="1">1</item>
<item id="2" answer="t" value="2">2</item>
<item id="3" answer="f" value="3">3</item>
<item id="4" answer="f" value="4">4</item>
</question>
</ed>
The error I receive intermittently is that "questionXML" is null or not an object.
Specifically this line when I call on the submitAns1() method:
questionXML = assessnode.getElementsByTagName("question")[edStage];
This solution works on FF, Opera, Chrome & Safari without any issues as far as I am aware.
Am I accessing the XML nodes incorrectly? I've been toying with fixes for this bug for weeks now, I'd really appreciate any insight into what I'm doing wrong.
On a side note, this is a personal project of mine - it's not university related - I'm much too old for that!
Any help greatly appreciated.
Regards,
Sykth.
© Stack Overflow or respective owner