$.parseXML() is not working in versions of IE greater than IE 8

I have a issue using $.parseXML() with more version of IE8. Its working absolutely fine in IE8 and chrome as well as Firefox. Please have a look on below given code.

Jquery:

        var result = data.d;
        var obj = decodeBase64(result);
        var xmlDoc = $.parseXML(obj);

XML returns:

<?xml version="1.0"?>
<ArrayOfMultiLineItemJson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema
    MultiLineItemJson>
    <Order_Number>PAAAA00017</Order_Number>
    <Order_Item>01</Order_Item>
    <Order_Subitem>0001</Order_Subitem>
    <Order_Type>PO</Order_Type>
    </MultiLineItemJson>
    </ArrayOfMultiLineItemJson>

Error message in IE9

Unhandled exception at line 490, column 3 in http://localhost:55005/resources/js/jquery-1.9.0.js

0x800a139e - Microsoft JScript runtime error: Invalid XML: 
<?xml version="1.0"?><ArrayOfMultiLineItemJson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><MultiLineItemJson><Order_Number>PAAAA00017</Order_Number><Order_Item>01</Order_Item></ArrayOfMultiLineItemJson>

Thanks in Advance

Answers:

Answer

the code that gives error can be traced to the jquery library code

// Cross-browser xml parsing
parseXML: function( data ) {
    var xml, tmp;
    if ( !data || typeof data !== "string" ) {
        return null;
    }
    try {
        if ( window.DOMParser ) { // Standard
            tmp = new DOMParser();
            xml = tmp.parseFromString( data , "text/xml" );
        } else { // IE
            xml = new ActiveXObject( "Microsoft.XMLDOM" );
            xml.async = "false";
            xml.loadXML( data );
        }
    } catch( e ) {
        xml = undefined;
    }
    if ( !xml || !xml.documentElement || xml.getElementsByTagName( "parsererror" ).length ) {
        jQuery.error( "Invalid XML: " + data );
    }
    return xml;
},

the later browsers ie 9,10 return window.DOMParser and the DOMParser(); object , and thus the code enters non-ie part . But ie 8 does not have DOMParser() defined , hence it throws error.

the fix would be add ie8 specific test alongwith if ( window.DOMParser ) Let me know if that helps

Answer

Have to put this in an answer because it doesn't fit in a comment. I can't reproduce this on a windows machine.

js code:

    $.get("test.txt", function (data) {
        console.log(data);
        //var obj = decodeBase64(result);
        var xmlDoc = $.parseXML(data);
        console.log(xmlDoc);
    });

xml:

<?xml version="1.0"?>
<ArrayOfMultiLineItemJson 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <MultiLineItemJson>
    <Order_Number>PAAAA00017</Order_Number>
    <Order_Item>01</Order_Item>
    <Order_Subitem>0001</Order_Subitem>
    <Order_Type>PO</Order_Type>
    </MultiLineItemJson>
    </ArrayOfMultiLineItemJson>

response header for test.txt: Content-Type text/plain

When renaming the test.txt to test.xml I get content type text/xml and all the code I need to get a document object from that is:

    $.get("test.xml", function (xmlDoc) {
        console.log(xmlDoc);
    });

When using the invalid XML you've posted then it would not work in FF as well since I get an error there.

I didn't use decodeBase64 maybe you should test without that or console.log the text that it is returning (console.log(obj)). Press F12 in IE to open the developer tools and the console should be under the Script tab

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.