tags:

views:

394

answers:

5

I am trying to figure out how to process XML in java script So i googled for it. The problem is , I don't know whether the tutorials I see will work only on IE.

What is the "Standard" way to process Xml data in java script?

Edit: Thanks for all your answers. I want to ask another question then. Is there some kind of 3rd party library which let me transperatly write JS code without worry about cross-browser functionality

+3  A: 

use the tutorials at w3schools.com. They indicate how to work with a variety of browsers

e.g. (a snippet)

try //Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async="false";
  xmlDoc.loadXML(txt);
  return xmlDoc;
  }
catch(e)
  {
  parser=new DOMParser();
  xmlDoc=parser.parseFromString(txt,"text/xml");
  return xmlDoc;
  }

Also, just to answer your direct question. The is a standard way (in the snippet above, it's in the catch block), but internet explorer doesn't support the standard way.... so you're stuck with something like the above

Jonathan Fingland
This only works with ActiveX enabled IE and Mozilla browsers.
ko-dos
+1  A: 

An easy approach would be to convert the XML to JSON format. (Google "xml to json" for several options.) If possible, I'd recommend converting to JSON on the server side. It's more compact than XML, which translates to faster download times. It's also much less processor-intensive to parse, so it will run faster on the client side.

Note that if you are working with small amounts of XML content, the difference will be negligable.

Brandon Gano
+2  A: 

Another option is to use a third-party library, such as Google's AjaXSLT, to abstract out the browser-specific code. This means you can then just concentrate on calling standard DOM methods, as defined in the XML spec. Google's parser is the only one that I have personal experience of, so I'm unsure whether it is the best available. Perhaps other people may be able to recommend other parsers?

Phil Booth
+4  A: 

Maybe you should take a look at sarissa. it is a crossbrowser library that i find very useful and easy to use. It permits you to

  • load XML from URLs or strings,
  • perform XSLT transformations,
  • apply XPath queries

Works on the major browsers and it is licensed under GPL

Eineki
A: 

The "standard" way to process XML in Javascript is to use one or more standard or broadly available APIs. The most widely adopted APIs for that are:

  • DOMParser object, allows for parsing an XML string into a DOM structure
  • XMLSerializer object, serializes DOM structure to XML string
  • XSLTProcessor object, enables XSLT processing
  • XMLHttpRequest object, to send XML over the wire

All of the mentioned objects are available in all of the modern (that are not IE) web browsers. By a lucky occasion, IE has also had implementations of these functionalities since ever (well, since IE5 or so), they just had different APIs. Since mentioned above objects are not available in IE it would be possible to implement them, so did Ample SDK and Sarissa projects, probably some others too.

For example, look how the code that enables cross-browser DOMParser may look:

if (!window.DOMParser) {
    var cDOMParser = function(){};
    cDOMParser.prototype.baseURI = null;
    cDOMParser.prototype.parseFromString = function(sXml, sMime) {
     var oDocument   = new ActiveXObject("Microsoft.XMLDOM");
     oDocument.async   = false;
     oDocument.validateOnParse = false;
     oDocument.loadXML(sXml);

     return oDocument;
    };
    window.DOMParser = cDOMParser;
};
Sergey Ilinsky