Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to save xml files using Javascript?

Tags:

javascript

xml

I tried to

1) load an xml file using javascript as an object, say note.xml

2) then save the object to a new xml file, say note_new.xml

I did 1) but failed 2)

I tried to use method save() to do 2). After my failure, I checked ms site and they said save() is not supported....

could some one enlighten me how to do the save?

thank you!

here is the code:

<html>
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1>W3Schools Internal Note</h1>
<p><b>To:</b> <span id="to"></span><br />
<b>From:</b> <span id="from"></span><br />
<b>Message:</b> <span id="message"></span>

<script type="text/javascript">
if (window.ActiveXObject){
alert("there is ActiveXObject");
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false; 
xmlDoc.load("note.xml"); 
}else{
alert("i am not withActiveXObject!");
xhttp=new XMLHttpRequest();
xhttp.open("GET","note.xml",false);
xhttp.send("");
xmlDoc=xhttp.responseXML;
}
xmlDoc.save("note_new.xml"); 
</script>

</body>
</html>

update:

seems this is related to security issue. I appologize to those experienced programmers for my putting this question in a rush because it seems a newbie question.

like image 625
john Avatar asked May 09 '10 13:05

john


People also ask

Can you use XML with JavaScript?

With a few lines of JavaScript code, you can read an XML file and update the data content of any HTML page.

How do I save HTML File as XML?

Simply click the File button (the 3 lines), and click Save Page As. For example, I went to xml-sitemaps.com/sitemap.xml and clicked Save Page As. It saved as XML to my local machine and loaded as such. Without any HTML.

How do I save an XML File in Notepad?

To save the XML document, on the File menu, click Save. To exit XML Notepad, on the File menu, click Exit.


1 Answers

Your problem is: javaScript does not have an input/output (I/O) API as it is a client-side scripting language and consequently has no access to the file system via the server. You would need to use a server-side scripting language to save data to a server. There may be hacks to solve your problem client-side, but they are probably either unsave or otherwise buggy. (btw: what api is the save method member of? Did you make that up?)

What you can do is save data temporarily to any DOM element (e.g. window, or a javaScript) object. There is however no way to make these changes permanent.

In your case, looking in to PHP scripting might be the best way to go.

like image 109
FK82 Avatar answered Oct 03 '22 01:10

FK82