jquery.xmldom.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*!
  2. * jQuery xmlDOM Plugin v1.0
  3. * http://outwestmedia.com/jquery-plugins/xmldom/
  4. *
  5. * Released: 2009-04-06
  6. * Version: 1.0
  7. *
  8. * Copyright (c) 2009 Jonathan Sharp, Out West Media LLC.
  9. * Dual licensed under the MIT and GPL licenses.
  10. * http://docs.jquery.com/License
  11. */
  12. (function($) {
  13. // IE DOMParser wrapper
  14. if ( window['DOMParser'] == undefined && window.ActiveXObject ) {
  15. DOMParser = function() { };
  16. DOMParser.prototype.parseFromString = function( xmlString ) {
  17. var doc = new ActiveXObject('Microsoft.XMLDOM');
  18. doc.async = 'false';
  19. doc.loadXML( xmlString );
  20. return doc;
  21. };
  22. }
  23. $.xmlDOM = function(xml, onErrorFn) {
  24. try {
  25. var xmlDoc = ( new DOMParser() ).parseFromString( xml, 'text/xml' );
  26. if ( $.isXMLDoc( xmlDoc ) ) {
  27. var err = $('parsererror', xmlDoc);
  28. if ( err.length == 1 ) {
  29. throw('Error: ' + $(xmlDoc).text() );
  30. }
  31. } else {
  32. throw('Unable to parse XML');
  33. }
  34. } catch( e ) {
  35. var msg = ( e.name == undefined ? e : e.name + ': ' + e.message );
  36. if ( $.isFunction( onErrorFn ) ) {
  37. onErrorFn( msg );
  38. } else {
  39. $(document).trigger('xmlParseError', [ msg ]);
  40. }
  41. return $([]);
  42. }
  43. return $( xmlDoc );
  44. };
  45. })(jQuery);