Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery XML Parsing IE7 / IE8

I'm trying to access XML content attached to the end of a HTML document (generated stuff) with jquery using this method:

$("SELECTION_STATE").find("CHARACTERISTIC").each( function() {
     if($(this).attr("name") == "Z_MDST" ) {
         alert($(this).find("MEMBER").attr("name"));
     }
 });

this works fine in Firefox and Chrome but not in IE, it won't alert anything.

this is the xml I'm trying to traverse

<SELECTION_STATE>

    <SELECTION type="CARTESIAN_PRODUCT">
      <CHARACTERISTICS>
        <CHARACTERISTIC name="Z_MDST">
          <SELECTIONS>
            <SELECTION type="SINGLE_MEMBER">
              <MEMBER name="00002213" type="MEMBER" text="2213"/>
            </SELECTION>
          </SELECTIONS>
        </CHARACTERISTIC>

is there any way I can achieve that with jquery 1.5?

Thanks in advance

like image 779
Stefan Ernst Avatar asked Mar 30 '11 12:03

Stefan Ernst


1 Answers

Because you are in a HTML document. IE won't recognize XML.

console.log($("SELECTION_STATE").get());

returns object HTMLUnknownElement in IE

In order to use the XML you'll have to run it through the IE XML parser. Something like.

var x = new ActiveXObject("Microsoft.XMLDOM");
x.loadXML(yourXML) 

You'll obviously only want to do this if($.browser.msie)

Side question: Are you loading the XML with AJAX?

Updated: Full Example

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
var myXML = document.body.innerHTML;  // or wherever you are storing the XML in the DOM
xmlDoc.loadXML(myXML)

if (xmlDoc.parseError.errorCode != 0) {
   var myErr = xmlDoc.parseError;
   console.log("You have error " + myErr.reason);
} else {
   console.log(xmlDoc.xml);
}

$("SELECTION_STATE", xmlDoc).find("CHARACTERISTIC").each( function() {
     if($(this).attr("name") == "Z_MDST" ) {
         alert($(this).find("MEMBER").attr("name"));
     }
});
like image 125
Daniel Avatar answered Nov 15 '22 09:11

Daniel