How to create and use a XML document object properly considering browser compatibility?

随声附和 提交于 2020-01-04 09:04:04

问题


Now I'm working on a web project. I need to create a XML document object using a given string buffer in JavaScript. I've successfully made it run smoothly on IE, but apparently I need to do some more work to improve its compatibility.

Here is a tiny example describing what I want to express (Note, all in JavaScript) first of all, we have a string variable, say, "buffer", which has been obtained from the server and, in fact, it is formed like a XML:

"<Messages><Item>aaa</Item><Item>bbb</Item></Messages>"

Then, I can use the following code segment to create a IE-recognizable XML doc object:

var xmlDoc = new ActiveXObject("Microsoft.xmlDOM");
xmlDoc.async = false;
xmlDoc.loadXML(buffer);

And we've got it.

So, what I want to know is how to create an object considering browser compatibility (firefox, opera, etc) and whether the usages of it are same.


回答1:


DOMParser should work. It is not a standard, but it is supported at least on WebKit (Safari, Chrome, etc), and Gecko (Firefox); I don't know about Opera:

var buffer = "<Messages><Item>aaa</Item><Item>bbb</Item></Messages>";
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(buffer, "text/xml");


来源:https://stackoverflow.com/questions/790370/how-to-create-and-use-a-xml-document-object-properly-considering-browser-compati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!