简体   繁体   中英

Parsing XML in IE with jQuery

I am having some trouble parsing xml with jQuery. I have a fragment of xml text and I would like to extract a certain node's value. But this isn't working in IE. It works fine in Chrome.

$(function () {
    var xmlText = '<?xml version="1.0" encoding="UTF-8" ?><dog_info><keyspec><keycaps><aes /><v-clock /></keycaps><dog>';
    xmlText += '<dogid>566578105</dogid><memoryinfo><access>read/write</access>';
    xmlText += '<fileid>65524</fileid><size>128</size></memoryinfo></dog></keyspec></dog_info>';
    alert($(xmlText).find("dogid").text()); //this do not work in ie
})

The problem could be the use of html parsing for xml content, so try to use $.parseXML()

$(function () {
    var xmlText = '<?xml version="1.0" encoding="UTF-8" ?><dog_info><keyspec><keycaps><aes /><v-clock /></keycaps><dog>';
    xmlText += '<dogid>566578105</dogid><memoryinfo><access>read/write</access>';
    xmlText += '<fileid>65524</fileid><size>128</size></memoryinfo></dog></keyspec></dog_info>';

    var doc = $.parseXML(xmlText);
    var $xml = $(doc);

    alert($xml.find("dogid").text()); //this do not work in ie
})

Can you try this?

 <script src="jquery.min.js"></script>
<script>
    $(function(){
        var xmlText= '<?xml version="1.0" encoding="UTF-8" ?><dog_info><keyspec><keycaps><aes /><v-clock /></keycaps><dog>';
        xmlText += '<dogid>566578105</dogid><memoryinfo><access>read/write</access>';
        xmlText += '<fileid>65524</fileid><size>128</size></memoryinfo></dog></keyspec></dog_info>';
        var xmlDoc = $.parseXML(xmlText); // convert string to xml document first
        alert($(xmlDoc).find("dogid").text());//this do not work in ie
    })
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM