javascript如何更改本地xml文件内容

<html>
<head>
<script type="text/javascript" src="loadxmldoc.js">
</script>
</head>
<body>

<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");

var x=xmlDoc.getElementsByTagName('book');
var newel,newtext

for (i=0;i<x.length;i++)
{
newel=xmlDoc.createElement('edition');
newtext=xmlDoc.createTextNode('First');
newel.appendChild(newtext);
x[i].appendChild(newel);
}

//Output all titles and editions
var y=xmlDoc.getElementsByTagName("title");
var z=xmlDoc.getElementsByTagName("edition");
for (i=0;i<y.length;i++)
{
document.write(y[i].childNodes[0].nodeValue);
document.write(" - Edition: ");
document.write(" <br/>");
document.write(z[i].childNodes[0].nodeValue);
document.write("<br />");
}
</script>
</body>
</html>
w3c直接复制的,可以在html里显示更改但是xml文件中依然不变

JS不能更改“本地文件”,因为安全限制。追问

那就是说它就只有浏览功能吧,那有什么意义

追答

意义大了呀,Web一直都不能修改本地文件——除了微软的IE——可这显然没有妨碍Web的飞速发展。顺便一提,Html5曾经一度考虑过引入“文件API”,以允许JS程序在极度受限的环境下修改本地文件,但最终W3C否决了文件API,所以预计在未来相当长的时间里,都不会有符合标准的、跨浏览器的修改本地文件的方法。

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-04-13
xmlDoc 对象内存中已修改了,只是没有保存。
function SaveInfoToFile(folder, fileName) {
var filePath = folder + fileName;
var fileInfo = "hahahaha";
var fso = new ActiveXObject("Scripting.FileSystemObject");
var file = fso.CreateTextFile(filePath, true);
file.Write( 你的XML字符串);
file.Close();
}

要注意IE的安全性设置本回答被网友采纳
相似回答