Search code examples
jqueryparsexml

How to add jquery Xml parsing functionality in older Jquery version file?


I have older version of Jquery in my application. i need ParseXML functionality to be included in it ( If i add the newwer version of jquery 1.8.2 the application is showing script error ,since lot of many other dependant plugins are written using the older version of Js file).I will be very thankfull if any one could provide me a solution for adding the ParseXML functionality in the older version of Jquery file.


Solution

  • you can browse the source of jQuery on git hub and import the parseXML functionality like

    $(document).ready(function(){
     jQuery.extend({
         parseXML: function(data) {
            var xml, tmp;
        if ( !data || typeof data !== "string" ) {
            return null;
            }
        try {
        tmp = new DOMParser();
        xml = tmp.parseFromString( data , "text/xml" );
        } catch ( e ) {
        xml = undefined;
        }
        if ( !xml || xml.getElementsByTagName( "parsererror" ).length ) {
        jQuery.error( "Invalid XML: " + data );
        }
        return xml;  
        }
     })
    
    var xml = "<rss version='2.0'><channel><title>RSS Title</title></channel></rss>",
    xmlDoc = $.parseXML( xml ),
    $xml = $( xmlDoc ),
    $title = $xml.find( "title" );
    console.log($title.text());
    });
    

    DEMO