I have a javascript function which I try to read XML file.
function readXML()
{
alert("readXML");
if(xmlDoc.readyState == 4 || xmlDoc.readyState == 'complete')
{
for(var i=0; i < xmlDoc.getElementsByTagName("Question").length; i++)
{
var CurrentTuple = xmlDoc.getElementsByTagName("Question")[i];
var QuestionID = CurrentTuple.attributes.getNamedItem("QuestionID").value;
var CorrectAnswer = CurrentTuple.attributes.getNamedItem("CorrectAnswer").value;
alert(QuestionID +":"+ CorrectAnswer);
var OutputDisplayString = "";
for(var j=0; j< CurrentTuple.childNodes.length; j++)
{
//alert(CurrentTuple.childNodes[j].nodeName);
OutputDisplayString += CurrentTuple.childNodes[j].nodeName;
OutputDisplayString += "\n";
}
alert(OutputDisplayString);
}
}
}
For XML FILE…
<?xml version="1.0" encoding="ISO-8859-1"?>
<Exam>
<Question QuestionID="Q001" CorrectAnswer="A">
<Description>Does a final member variable have to be initialized at the time it's declared?</Description>
<AnswerA>No</AnswerA>
<AnswerB>Yes</AnswerB>
<AnswerC></AnswerC>
<AnswerD></AnswerD>
</Question>
</Exam>
Then, please let me show output result of “alert(OutputDisplayString);” with Firefox.
#text
Description
#text
AnswerA
#text
AnswerB
#text
AnswerC
#text
AnswerD
#text
Let me make compare the result which i get by using IE.
---------------------------
Message from webpage
---------------------------
Description
AnswerA
AnswerB
AnswerC
AnswerD
---------------------------
OK
---------------------------
So What i would like to know is what is #text which i get as a result from Firefox.
Then by getting this #text result , I cannot evaluate the exact count of “CurrentTuple.childNodes.length” childNotes.
Please let me know what is happening the result of #text by Firefox.
And how can i get correct count of childNotes.
Firefox counts white spaces and line breaks as XML nodes while IE doesn’t. If you write
on the same line in your XML document you should find no text node between AnswerA and AnswerB.
I would filter out text nodes manually as they are iterated like this:
or rather like this:
Other methods I can think of don’t work on older browsers.