javascript - 使用 javascript 创建和修改 xml 文件(想在客户端保存 xml 文件)

标签 javascript xml

如何使用 javascript 在客户端保存 xml 文件...?

        if (window.XMLHttpRequest) {
            xhttp = new XMLHttpRequest();
        }
        else // Internet Explorer 5/6
        {
            xhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xhttp.open("GET", "User.xml", false);
        xhttp.send("");
        xmlDoc = xhttp.responseXML;

        xmlDoc.getElementsByTagName("Username")[0].childNodes[0].nodeValue = 'asdasf';
        xmlDoc.getElementsByTagName("password")[0].childNodes[0].nodeValue = 'asdAS';
        xmlDoc.getElementsByTagName("UserId")[0].childNodes[0].nodeValue = '12';
        xmlDoc.save("User.xml");

这不起作用..显示错误,因为保存不是函数。

谢谢 苏妮莎。

最佳答案

这在 JavaScript 中是不允许的。这将是一个安全漏洞。

阅读JavaScript security

JavaScript and the DOM provide the potential for malicious authors to deliver scripts to run on a client computer via the web. Browser authors contain this risk using two restrictions. First, scripts run in a sandbox in which they can only perform web-related actions, not general-purpose programming tasks like creating files. Second, scripts are constrained by the same origin policy: scripts from one web site do not have access to information such as usernames, passwords, or cookies sent to another site. Most JavaScript-related security bugs are breaches of either the same origin policy or the sandbox.

关于javascript - 使用 javascript 创建和修改 xml 文件(想在客户端保存 xml 文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1795615/

相关文章:

c# - 构建一个 "scriptable"应用程序

xml - 为什么我的 ASP.Net Core Web API Controller 不返回 XML?

javascript - 如何根据查询返回选择html下拉列表

javascript - 如何使用 webpack 包含整个 polyfill 文件夹?

javascript - 如何将 float 格式化为字符串而没有不必要的小数 0?

javascript - 在电子商务网站中添加多个商品到购物车(add to cart)

javascript - 如果元素被不同的脚本更改,则动态更改该元素

c# - 如何使用 LINQ to XML 获取属性值?

xml - 我应该如何在 Perl 中解析大型 XML 文件?

xml - XSLT 和 XSL-FO : restructure XML data to dynamically create page-sequences for every page