Saving Newly Modified DOM Structures to an XML File
I know how to use AJAX to load specific XML files but i need to learn how to save the DOM object back to the original XML file after it has been modified. Say if i was to use this code.
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function()
{
if(xhr.readyState == 4 && xhr.status == 200)
{
var xmldom = xhr.responseXML;
var root = xmldom.getElementsByTagName("root");
var newElement = xmldom.createElement("newElement");
root.appendChild(newElement);
}
}
xhr.open("GET", "file.xml", true);
xhr.send(null);
The XML is as follows:
<root>
</root>
Any help would be greatly appreciated.
Last edited by jblevins1991; 09-11-2012 at 09:47 PM.
Thank you but i still don't quite understand fully. Ive read about the serializeToString(). After using that to get the xmldom object into a string how would i save it to the file. The documentation in that file is very mediocre and confusing.
Last edited by jblevins1991; 09-11-2012 at 10:16 PM.
To send data back to the server whether is plain text, xml or json encoded data, you should POST instead of GET.
xhr.open("POST", "url"); //url of your server-side script to receive the XML data
xhr.send(xmldoc); //xmldoc is your XML formatted document
The problem with this is that we don't have an instance to the xml document until the ready state is 4, after the send method is called. This would not do a thing. The send method is mainly used to send form data to the server then the server would respond with the result from a php, asp, or xml file. i would have to save the dom structure to the xml file after appendChild is called.
to save your xml in a file you need a tool which has feature to write data in files for example PHP. as PHP is simple easy and commonly used language. you can send your xml to a php script which will store it in a file. its as simple.
That is not an issue at all. Just write. A function that will be called after then xmlml document has been created just before the closing curly brace that end the
if(...){ ....}
block.
You should always use functions to keep your code clean and portable.
The file is already created seeing as it is not practical to create a new file everytime you need to write to it. I need to overwrite the old files contents with the dom structure that was modified. I need to use javscript not php.
I need to overwrite the old files contents with the dom structure that was modified. I need to use javscript not php.
look into node.js then; it's javascript. haha...
the short answer is that javascript can't save your file.
the long answer is that it maybe can be done simply and for free, but like many things in programming, it depends.
tell us more about the task:
is this just for you?
what kind of server are you using?
what browser(s) does this have to work for?
does it need to work in windows or mac?
with these questions answered, i might be able to provide a solution for you, but it's impossible to say without knowing more...
the short answer is that javascript can't save your file.
the long answer is that it maybe can be done simply and for free, but like many things in programming, it depends.
tell us more about the task:
is this just for you?
what kind of server are you using?
what browser(s) does this have to work for?
does it need to work in windows or mac?
with these questions answered, i might be able to provide a solution for you, but it's impossible to say without knowing more...
I has to work for all browsers and all operating systems. I have a hard time believing that javascript cannot save differences to a file.
I has to work for all browsers and all operating systems. I have a hard time believing that javascript cannot save differences to a file.
what, do you think all us posters and google are conspiring to keep a file-saving routine secret? nothing works on all OSs; iOS doesn't event have a file system to save to! ajax was invented to load mail messages into a web client, FORMs have always been the #1 way of saving data, and forms send data to servers. would you really want any old site saving files onto your computer?
Javascript was not built to work with files or similar. it was built to work inside the browser to provide client side functionality to websites.
to save files you will always need a tool that was built for working with files or atleast has a functionality.
allowing websites to store data directly on your computer can be dangerous. any web site can store viruses etc... on your computer, and i think no one wants it.
I had to think about it for a second lol. client sided+should not have access to server sided content=dum dum. I have figured out how to do this with php. I will link the threads where i posted the php code so others may see that when they look this thread up in the search. Thank you for all of the help. I appreciate it.
Bookmarks