www.webdeveloper.com
Results 1 to 14 of 14

Thread: Saving Newly Modified DOM Structures to an XML File

  1. #1
    Join Date
    Aug 2012
    Posts
    23

    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 10:47 PM.

  2. #2
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787

  3. #3
    Join Date
    Nov 2006
    Location
    Oakland
    Posts
    500
    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

  4. #4
    Join Date
    Aug 2012
    Posts
    23
    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 11:16 PM.

  5. #5
    Join Date
    Aug 2012
    Posts
    23
    Quote Originally Posted by holyhttp View Post
    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.

  6. #6
    Join Date
    Mar 2009
    Posts
    452
    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.

  7. #7
    Join Date
    Nov 2006
    Location
    Oakland
    Posts
    500
    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.

  8. #8
    Join Date
    Aug 2012
    Posts
    23
    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.

  9. #9
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by jblevins1991 View Post
    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...

  10. #10
    Join Date
    Aug 2012
    Posts
    23
    Quote Originally Posted by rnd me View Post
    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...
    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.

  11. #11
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by jblevins1991 View Post
    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?
    Last edited by rnd me; 09-14-2012 at 01:33 PM.

  12. #12
    Join Date
    Aug 2012
    Posts
    23
    No it just seems like after years there would be a simple way to save or overwrite files. I did not mean to offend you.

  13. #13
    Join Date
    Mar 2009
    Posts
    452
    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.

  14. #14
    Join Date
    Aug 2012
    Posts
    23
    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.

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles