简体   繁体   English

如何从JavaScript更改XML元素值?

[英]How can I change an XML element value from JavaScript?

I have an XML DB and I have been able to parse the XML using DOM with JS. 我有一个XML DB,并且能够使用JS的DOM解析XML。 The thing I am unable to figure out is, how can I change a value in my XML db? 我无法弄清楚的是,如何更改XML数据库中的值?

I tried using: 我尝试使用:

xmlDoc.getElementsByTagName("COMMENT")[0].childNodes[0].nodeValue="text"; 

But this does not changes the actual DB. 但这不会更改实际的数据库。 When I refresh my page, it gives me the same old value again. 当我刷新页面时,它再次为我提供了相同的旧值。

What am I doing wrong? 我究竟做错了什么?


Edit: 编辑:

I am making changes only on the client page and not sending the data back to make relevant changes in database itself. 我仅在客户端页面上进行更改,而没有将数据发送回数据库中进行相关更改。

I understand I should use AJAX or something, but could you please give me directions on what I should read or some examples where I can learn? 我了解我应该使用AJAX或其他工具,但是能否请您给我一些指导,以指导我阅读哪些内容或可以学习的示例?

You cannot write to XML with JavaScript, only load a copy of the XML DOM into memory and manipulate that copy. 您不能使用JavaScript编写XML,只能将XML DOM的副本加载到内存中并操作该副本。 Obviously that is destroyed when the JS instance restarts (reloading the page) or you re-load from the original file. 显然,当JS实例重新启动(重新加载页面)或从原始文件重新加载时,该销毁了。

If the XML is on the server, you will need a server-side language such as PHP, ASP.NET, Ruby on Rails, etc. to write anything to disk on the server. 如果XML在服务器上,则需要服务器端语言(例如PHP,ASP.NET,Ruby on Rails等)将任何内容写入服务器上的磁盘。 That code executes on the server, not in the browser. 该代码在服务器上执行,而不是在浏览器中执行。

To communicate between your JavaScript code and your server-side code, AJAX is the answer. 为了在JavaScript代码和服务器端代码之间进行通信,AJAX是答案。 A List Apart has a good resource for getting started . List Apart有很好的入门资源 The simplest model that comes to mind is using AJAX to send the complete, modified XML chunk to server-side code, which simply saves it. 想到的最简单的模型是使用AJAX将完整的,经过修改的XML块发送到服务器端代码,从而将其保存。

If you are dealing with huge files and frequent updates, you may want to consider using AJAX to send manipulation instructions to the server-side code, which execute the changes and save the file. 如果要处理大文件和频繁更新,则可能要考虑使用AJAX将操作指令发送到服务器端代码,该代码将执行更改并保存文件。

您基本上没有告诉我们任何相关信息,但是很可能您只是在更改客户端页面,并且不做任何努力(例如使用表单或AJAX)发回更改。

You need to save db after changes. 更改后需要保存数据库。 It is not possible with DOM and JS on Web Browser, so you should use AJAX or sth similar to process XML Web浏览器上的DOM和JS无法使用,因此您应使用AJAX或类似的东西来处理XML

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM