如何使用Javascript保存xml文件?

时间:2022-04-01 11:26:56

I tried to

我尝试过了

1) load an xml file using javascript as an object, say note.xml

1)使用javascript作为对象加载xml文件,比如note.xml

2) then save the object to a new xml file, say note_new.xml

2)然后将对象保存到一个新的xml文件,比如note_new.xml

I did 1) but failed 2)

我做了1)但失败了2)

I tried to use method save() to do 2). After my failure, I checked ms site and they said save() is not supported....

我尝试使用方法save()来做2)。我失败后,我检查了ms网站,他们说不支持save()....

could some one enlighten me how to do the save?

有人可以告诉我如何保存?

thank you!

谢谢!

here is the code:

这是代码:

<html>
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1>W3Schools Internal Note</h1>
<p><b>To:</b> <span id="to"></span><br />
<b>From:</b> <span id="from"></span><br />
<b>Message:</b> <span id="message"></span>

<script type="text/javascript">
if (window.ActiveXObject){
alert("there is ActiveXObject");
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false; 
xmlDoc.load("note.xml"); 
}else{
alert("i am not withActiveXObject!");
xhttp=new XMLHttpRequest();
xhttp.open("GET","note.xml",false);
xhttp.send("");
xmlDoc=xhttp.responseXML;
}
xmlDoc.save("note_new.xml"); 
</script>

</body>
</html>

update:

更新:

seems this is related to security issue. I appologize to those experienced programmers for my putting this question in a rush because it seems a newbie question.

似乎这与安全问题有关。我向那些经验丰富的程序员道歉,因为这似乎是一个新手问题。

1 个解决方案

#1


7  

Your problem is: javaScript does not have an input/output (I/O) API as it is a client-side scripting language and consequently has no access to the file system via the server. You would need to use a server-side scripting language to save data to a server. There may be hacks to solve your problem client-side, but they are probably either unsave or otherwise buggy. (btw: what api is the save method member of? Did you make that up?)

您的问题是:javaScript没有输入/输出(I / O)API,因为它是客户端脚本语言,因此无法通过服务器访问文件系统。您需要使用服务器端脚本语言将数据保存到服务器。客户端可能存在解决问题的黑客攻击,但它们可能是无法解决的,也可能是其他问题。 (顺便说一下:什么api是保存方法的成员?你做到了吗?)

What you can do is save data temporarily to any DOM element (e.g. window, or a javaScript) object. There is however no way to make these changes permanent.

您可以做的是将数据临时保存到任何DOM元素(例如窗口或javaScript)对象。然而,没有办法使这些变化永久化。

In your case, looking in to PHP scripting might be the best way to go.

在您的情况下,查看PHP脚本可能是最好的方法。

#1


7  

Your problem is: javaScript does not have an input/output (I/O) API as it is a client-side scripting language and consequently has no access to the file system via the server. You would need to use a server-side scripting language to save data to a server. There may be hacks to solve your problem client-side, but they are probably either unsave or otherwise buggy. (btw: what api is the save method member of? Did you make that up?)

您的问题是:javaScript没有输入/输出(I / O)API,因为它是客户端脚本语言,因此无法通过服务器访问文件系统。您需要使用服务器端脚本语言将数据保存到服务器。客户端可能存在解决问题的黑客攻击,但它们可能是无法解决的,也可能是其他问题。 (顺便说一下:什么api是保存方法的成员?你做到了吗?)

What you can do is save data temporarily to any DOM element (e.g. window, or a javaScript) object. There is however no way to make these changes permanent.

您可以做的是将数据临时保存到任何DOM元素(例如窗口或javaScript)对象。然而,没有办法使这些变化永久化。

In your case, looking in to PHP scripting might be the best way to go.

在您的情况下,查看PHP脚本可能是最好的方法。