Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cross-browser standard Xml processing in Java Script

Tags:

javascript

xml

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

like image 771
user88637 Avatar asked Jun 27 '09 07:06

user88637


4 Answers

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

like image 184
Eineki Avatar answered Nov 08 '22 07:11

Eineki


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?

like image 34
Phil Booth Avatar answered Nov 08 '22 08:11

Phil Booth


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

like image 44
Jonathan Fingland Avatar answered Nov 08 '22 09:11

Jonathan Fingland


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;
};
like image 1
Sergey Ilinsky Avatar answered Nov 08 '22 09:11

Sergey Ilinsky