[英]How do I serialize a DOM to XML text, using JavaScript, in a cross browser way?
I have an XML object (loaded using XMLHTTPRequest
's responseXML
). 我有一个XML对象(使用
XMLHTTPRequest
的responseXML
加载)。 I have modified the object (using jQuery) and would like to store it as text in a string. 我修改了对象(使用jQuery)并希望将其作为文本存储在字符串中。
There is apparently a simple way to do it in Firefox et al: Firefox等人显然有一种简单的方法:
var xmlString = new XMLSerializer().serializeToString( doc );
(from rosettacode ) (来自rosettacode )
But how does one do it in IE6 and other browsers (without, of course, breaking Firefox)? 但是如何在IE6和其他浏览器中做到这一点(当然,没有破坏Firefox)?
You can use doc.xml in internet exlporer. 您可以在Internet exlporer中使用doc.xml。
You'll get something like this: 你会得到这样的东西:
function xml2Str(xmlNode) {
try {
// Gecko- and Webkit-based browsers (Firefox, Chrome), Opera.
return (new XMLSerializer()).serializeToString(xmlNode);
}
catch (e) {
try {
// Internet Explorer.
return xmlNode.xml;
}
catch (e) {
//Other browsers without XML Serializer
alert('Xmlserializer not supported');
}
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.