最近,在客户端解吸xml时候,需要将XML转化成字符串,看了很多文章,介绍的最多的是将XML字符串转化成对象。解决方法如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>xml转化成字符串</title>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
//使fireFox也支持xmlDom.xml
if (!document.all) {
Node.prototype.__defineGetter__(
"xml",
function () {
return (new XMLSerializer).serializeToString(this);
});
}
alert(1);
var xmlStr = "<test>Hello world</test>";
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(xmlStr, "text/xml");
alert(xmlDoc.documentElement.nodeName);
alert(xmlDoc.xml);
});
</script>
</head>
<body>
</body>
</html>