Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross-Browser Javascript XML Parsing

Are there any cross-browser / cross-platform ways to parse XML files in Javascript?

like image 561
sazr Avatar asked Oct 31 '11 03:10

sazr


People also ask

Can JavaScript parse XML?

XML parsing in JavaScript is defined as it is one kind of package or library of the software which provides an interface for the client applications to work with an XML document and it is used in JavaScript to transform the XML document into readable form, nowadays in many browsers, the XML parser is already available ...

Can browser parse XML?

All major browsers have a built-in XML parser to access and manipulate XML.

Can XML parse HTML?

You can try parsing an HTML file using a XML parser, but it's likely to fail. The reason is that HTML documents can have the following HTML features that XML parsers don't understand. XML parsers will fail to parse any HTML document that uses any of those features.


2 Answers

The following will work in all major browsers, including IE 6:

var parseXml;  if (typeof window.DOMParser != "undefined") {     parseXml = function(xmlStr) {         return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml");     }; } else if (typeof window.ActiveXObject != "undefined" &&        new window.ActiveXObject("Microsoft.XMLDOM")) {     parseXml = function(xmlStr) {         var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");         xmlDoc.async = "false";         xmlDoc.loadXML(xmlStr);         return xmlDoc;     }; } else {     throw new Error("No XML parser found"); } 

Example usage:

var xml = parseXml("<foo>Stuff</foo>"); alert(xml.documentElement.nodeName); 

Live demo:

var parseXml;    if (typeof window.DOMParser != "undefined") {      parseXml = function(xmlStr) {          return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml");      };  } else if (typeof window.ActiveXObject != "undefined" &&         new window.ActiveXObject("Microsoft.XMLDOM")) {      parseXml = function(xmlStr) {          var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");          xmlDoc.async = "false";          xmlDoc.loadXML(xmlStr);          return xmlDoc;      };  } else {      throw new Error("No XML parser found");  }    var xml = parseXml("<foo>Stuff</foo>");  document.body.innerHTML = "Root element: " + xml.documentElement.nodeName;
like image 124
Tim Down Avatar answered Oct 03 '22 10:10

Tim Down


Consider using jQuery.parseXML.

Note that old JQuery's code (pre 2.x) is essentially identical to one proposed in the accepted answer and can be found at http://code.jquery.com/jquery-1.9.1.js, partial version below:

// Cross-browser xml parsing parseXML: function( data ) {     ...     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;     }     ... } 

Starting JQuery 2.x code changed to skip ActiveX branch, if you still need it - use older version of JQuery or inline ActiveX parsing. Partial code from http://code.jquery.com/jquery-2.0.0.js:

// Cross-browser xml parsing parseXML: function( data ) {     var xml, tmp;     .....     // Support: IE9     try {         tmp = new DOMParser();         xml = tmp.parseFromString( data , "text/xml" );     } catch ( e ) {         xml = undefined;     }     ..... }, 
like image 29
Alexei Levenkov Avatar answered Oct 03 '22 11:10

Alexei Levenkov