7

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.

5
  • Where do you want to save it? Client, server? Commented May 9, 2010 at 13:50
  • thank you, oedo, could you please tell me what I need to do? I really got very good answers to my previous questions.. Should I do something to these answers? If so....where to go? sorry I'm really new to this site... Commented May 9, 2010 at 14:01
  • To Nick: thanks! What I tried to do is really just to create a simple interface on my pc and use it only by myself. Since I want to use xml as data storage and javascript as driving engine, I looked for the method to upload and download. Seems Microsoft only allow VB to do the save...is there a security reason for not supporinting save() in other languages? thanks. Commented May 9, 2010 at 14:05
  • @user311884 - whichever answer you think is best, just click the checkmark associated with it to accept it. Commented May 9, 2010 at 14:14
  • yes, i got them! they are all very nice answers! thank you, SB, and oedo!! Commented May 9, 2010 at 14:17

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

4 Comments

nice answer. i was trying to avoid installing an addtional software ...thank you!
Hi, you're welcome! PHP is pretty accessible. So, no worries there. For what it's worth: for more complex applications there's absolutely no way around learning server-side scripting as you will need the capabilities of high-level programming languages such as Java (JSP/Servlets) or C# (ASP/.NET) for the logic of your application. If you split responsibilites between those and a client-side scripting (for user interaction) in an intelligent manner, then that will make things a lot easier (i.e. less convoluted, more structured) in the long run. So, there really is no good reason tu shun it.
Again, thank you for your wonderful advince! I'll try to bring PHP into the picture. have a great evening!
Welcome. Good luck with your project and thanks for accepting my answer!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.