I am trying to use jQuery to parse an XML document in memory. This works great in everything except IE (shocker). Some googling showed that the problem is most likely due to the fact that IE is treating my document as HTML, not XML MIME Type.
Is there a way to do the job of implementing jQuery, or do I need to check the client browser and implement specific XML XML analysis?
Thank!!
function getQAData(xmlData) {
var dataArr = new Array();
$(xmlData).find('item').each(function() {
dataArr.push({ questionid: $(this).attr("cellID"), answer: $(this).find('answer').text() });
});
return dataArr;
XML example
<hwroot>
<data pid="">
<item cellID="24_951">
<question>Are you there?</question>
<answer>No</answer>
</item>
<item cellID="24_957">
<question>A Question?</question>
<answer>blah blah blah</answer>
</item>
</data>
</hwroot>
Decision:
The jQuery.find () solution will not work for the reasons described below. Here is my solution:
Separator JS code based on navigator: (
if (browserName == "Microsoft Internet Explorer") {
MsXmlParse(xmlData, dataArr);
} else {
$(xmlData).find('item').each(function() {
dataArr.push({ questionid: $(this).attr("cellID"), answer: $(this).find('answer').text() });
});
}
MsXmlParse Definition
function MsXmlParse(xmlDocument, qarray) {
var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
xmldoc.async = "false";
xmldoc.loadXML(xmlDocument);
var itemElements = xmldoc.getElementsByTagName("item");
for (var i = 0; i < itemElements.length; i++) {
var questionCellID = itemElements[i].getAttributeNode("cellID").nodeValue;
var answerTxt = itemElements[i].childNodes[1].text;
qarray.push({ questionid: questionCellID, answer: answerTxt });
}
} // end msxmlparse
source
share