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
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
Works on the major browsers and it is licensed under GPL
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?
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
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:
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;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With