0

This is what I need:

I need to get an XML file from a server using the standard code:

if(window.XMLHttpRequest)
    xmlhttp = new XMLHttpRequest();
else
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // For IE6

xmlhttp.open("GET", xml_file, false);
xmlhttp.send();
xmldoc = xmlhttp.responseXML;

After this I need to make some changes to the XML information I received and save it to the server. Is this possible? Can I edit a file client side and send it to a server to replace the old file?

2
  • assuming that's a full-blown XML file, then you parse to a DOM tree, do your manipulations, convert back to a string, and post the string back to the server. There should never be a need to use an actual file, which standard JS has no concept of anyways. Commented Sep 17, 2012 at 14:28
  • Thank you also for your input. Commented Sep 17, 2012 at 15:31

1 Answer 1

1

Well, you can but you need a server side script that will accept the file (as a string) and save it.

You will need a url that will accept the contents of the file and write it on the server:

//site.com/writeXML.php

This file will accept the contents of the file(say as file_contents parameter) Now you have to send your contents to the file

//Do something with xmlDoc

if(window.XMLHttpRequest)
  xmlhttp = new XMLHttpRequest();
else
  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // For IE6
  xmlhttp.open("POST", "http://site.com/writeXML.php", false);
  xmlhttp.send("file_contents="+xmlDoc);

On server side, you need to accept the data and write it to the file as follows:

   $contents=$_POST['file_contents'];
   $handle = fopen("/home/user/data/xmlFile.xml, "wb");
   fwrite($handle, $contents);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This was something like what I needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.