JQuery Find () and XML do not work in IE

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; // this array is nice and populated in everything but ie

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

+5
source share
5 answers

I have no problem with this, as you can see here:

http://jsfiddle.net/treeface/VuwcH/

, IE XML , , xmlData. , - , DOMParser, XML- IE. :

xmlData = new ActiveXObject("Microsoft.XMLDOM");
xmlData.async = "false";
xmlData.loadXML(text); //where text is your XML string

Edit

, , xmlData XML? , "XML- ", IE. , xmlData . IE , HTML HTML node. jQuery XML- div, IE. jQuery , " ".

: http://bugs.jquery.com/ticket/3143

: : http://api.jquery.com/jQuery#jQuery2 jQuery html, xml. div, , IE . html.

, :

HTML , , , innerHTML. jQuery <div> innerHTML HTML, . , $('<img/>') $('<a></a>'), jQuery , JavaScript createElement().

HTML DOM, HTML-. , Internet Explorer 8 href URL-, Internet Explorer 9 HTML5 .

, " ", . ( ), , .

... ( ?)... , . , , JSON XML.

+5

jQuery normal jQuery()/$() , XML: HTML, . parseXML:

var xmlDoc = $.parseXML(xmlData);
+3

IE IE7? jQuery XML IE , .

. , , "cellID".

0
0

!!! ,

Chrome/Firefox:

xml.children[0].childNodes[1].innerHTML

IE8 +/Safari:

xml.childNodes[0].childNodes[1].textContent

IE8:

xml.documentElement.childNodes[1].text;

,

var xml = $.parseXML(XMLDOC); 

Var xmlNodeValue = ""; 

if(userAgent.match("msie 8.0")){

xmlNodeValue = xml.children[0].childNodes[1].innerHTML;

}else{ // IE8+

xmlNodeValue = xml.childNodes[0].childNodes[1].textContent; 

}
0

All Articles