PDA

View Full Version : Using appendChild with xml


woah
11-25-2006, 06:00 PM
This is my first experience with this code so I'm sorry if its a really dumb mistake but i can't figure it out.

I have a script which checks for IE/firefox etc, and then loads an xml file using: xmlDoc.load("pics.xml");

It then goes on to perform this very simple alteration:
var x = xmlDoc.getElementsByTagName("photos");
var newPic = xmlDoc.createElement("pic");
x[0].appendChild(newPic);

Neither IE or Firefox give me an error when this script runs, but when I check the XML file no changes have been made at all. It simply does not append the child and gives me no reason. Any help is much appreciated, thanks in advance.

1212jtraceur
11-25-2006, 08:43 PM
I don't know very much about this type of problem, but I'm guessing that your code doesn't actually alter the xml file, it only alters an object in the computer's memory (much like a javascript variable).

Kor
11-26-2006, 08:20 AM
javascript alone can not write/store data, as it is a client-side language. That means javascript changes only the HTML/javascript web page's elements/attributes/values, and only "virtually". Even if you "vitually alter" something with javascript, if you will try a View Sourse on your browser you will see no phisically change.

Once the session is over, the changes are lost.

Conclusion. You can not alter an XML with simple javascript. You must use an auxiliary server-side language (php, asp, java....) application for that.

Andras
12-09-2006, 12:34 AM
It may be a good idea to use Ajax for that, you can "modify using javascript" wahtever you want