开发者

jQuery XML Parsing IE7 / IE8

开发者 https://www.devze.com 2023-02-21 07:47 出处:网络
I\'m trying to access XML content attached to th开发者_Python百科e end of a HTML document (generated stuff) with jquery using this method:

I'm trying to access XML content attached to th开发者_Python百科e 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


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"));
     }
});


Since you are already using jQuery use the parseXML function(http://api.jquery.com/jQuery.parseXML/ added in version 1.5)

var xmlDoc = $.parseXML(data);

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

精彩评论

暂无评论...
验证码 换一张
取 消