PDA

View Full Version : appending to XML file with javascript - problem accessing XML file


mat106
08-10-2004, 11:48 AM
Hi.

I'm trying to add elements to an xml file i've created using a javascript script but whenever a run the script a get a "Permission denied" error for the line xmlDoc.save("phonebook2.xml"). Both the script and the xml file are on my hard drive so i don't understand why the script can't write to the file.

The script is below:

<html>
<head>
<title>phonebook</title>
</head>
<body>
<script type="text/javascript">

var xmlDoc= new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("phonebook2.xml")

var person = xmlDoc.createNode("element", "person", "");
xmlDoc.documentElement.appendChild(person);

var status = xmlDoc.createNode("element", "status", "");
person.appendChild(status);
status.text = "family";
var surname = xmlDoc.createNode("element", "surname", "");
person.appendChild(surname);
surname.text = "family";
var forename = xmlDoc.createNode("element", "forename", "");
person.appendChild(forename);
forename.text = "family";
var address = xmlDoc.createNode("element", "address", "");
person.appendChild(address);
address.text = "family";
var email = xmlDoc.createNode("element", "email", "");
person.appendChild(email);
email.text = "family";
var hometelno = xmlDoc.createNode("element", "hometelno", "");
person.appendChild(hometelno);
hometelno.text = "family";
var mobiletelno = xmlDoc.createNode("element", "mobiletelno", "");
person.appendChild(mobiletelno);
mobiletelno.text = "family";
var worktelno = xmlDoc.createNode("element", "worktelno", "");
person.appendChild(worktelno);
worktelno.text = "family";
xmlDoc.save("phonebook2.xml")
xmlDoc = null;
</script>
</body>
</html>

How can i solve the problem? Thanks.

jkd
08-10-2004, 01:12 PM
Try saving the document as .hta instead of .html, then try running it from the hard drive.

mat106
08-10-2004, 01:28 PM
jkd,

Thanks a lot. That has got me a step closer. However, what i intend to do is use a form to input the various variables (status, surname ... etc) and then pass the variables to the script which will then append them to the xml. The problem is that internet explorer does not recognise HTML application files and comes up with the usual "Open, Save, Cancel, More info" dialog box. Does this mean i'm going to have to have the form in an .htm file and have the action for the form pointing to the .hta file like this?

<form action="phonebook.hta" method="post">

Also, .hta files only work with IE. How can i make this work with other browser such as Mozilla?

Alex Vincent
08-12-2004, 03:09 AM
Repeating from another thread (I didn't realize that I'd get the same question twice this quickly :o )

If you mean saving it on the client machine, you're going to run into the security sandbox model. Your application will only be able to dodge this if (a) it is in a chrome:// URL, or (b) the user has given your application permissions to access your filesystem. If you're doing the former, jslib.mozdev.org has a great file i/o script.

If you mean saving it on the server, that will require a server-side script to accept the submission.