Zeeshan Rang
Zeeshan Rang

Reputation: 19875

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. The thing I am unable to figure out is, how can I change a value in my XML db?

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?

Upvotes: 0

Views: 8806

Answers (3)

Rex M
Rex M

Reputation: 144112

You cannot write to XML with JavaScript, only load a copy of the XML DOM into memory and manipulate that copy. Obviously that is destroyed when the JS instance restarts (reloading the page) or you re-load from the original file.

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. 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. A List Apart has a good resource for getting started. 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.

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.

Upvotes: 2

Tolgahan Albayrak
Tolgahan Albayrak

Reputation: 3206

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

Upvotes: 0

Matthew Flaschen
Matthew Flaschen

Reputation: 284786

You've told us basically no relevant info, but most likely you're just changing the client page, and making no effort to send back (e.g. with a form or AJAX) the changes.

Upvotes: 0

Related Questions