views:

106

answers:

1

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.

To help you understand my poor English, 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. Please help me!

+1  A: 

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");
Brian Campbell
Thank you very much! It works.What's more, you edit the answer for me. It's kind of you!
fwoncn