Click to See Complete Forum and Search --> : Opening and saving a XML file.


erevtsov
August 4th, 2009, 12:05 PM
My task is to open an XML file using JavaScript, be able to make changes to some fields if necessary, and then write the changes to the original file. I was able to open and read the file, but now I need to save the changes. I have the names and values of necessary parameters in an array. I am not new to programming but I am new to scripting. Please help.

PeejAvery
August 4th, 2009, 12:49 PM
JavaScript does not have any access to write to the file system unless you are using ActiveX. Then, you will be restricted to using Internet Explorer and changing the security settings.

erevtsov
August 4th, 2009, 01:00 PM
Sorry I didn't mention this before. I am aware that JavaScript doesn't have access to the file system. What can I do though to be able to save the file? What other language will I need to use?

PeejAvery
August 4th, 2009, 03:16 PM
Is there a reason why you are using JavaScript in the first place? Does this need to be client-side directly?

If it doesn't, you could easily use a server-side language such as PHP to parse the XML and change contents, then bring up a force dialog box for the client-side user.

erevtsov
August 5th, 2009, 11:40 AM
I will explain my task in better detail. I have an XML file (on the local drive) that has some parameters. I need to create a GUI so it's easier for users to edit the values of these parameters. So the user can open my GUI that displays the parameters of the XML, and he needs to be able to edit and save the values. I was using JavaScript because I know it fairly well and I thought it would be the easiest way. Also, I need this to run on multiple platforms and JavaScript would enable it to do so.

What would you suggest for me to do?

Xeel
August 5th, 2009, 01:30 PM
What do you mean when you say "multiple platforms"?

I can give a solution for Javascript using MS Windows XP API. Still it's obvious that the program wouldn't work under Linux for ex. The reason we are not giving the direct answer is that there is no cross-platform one. To read XML you need to access file system and this is achieved by implementing platform specific classes.

If you decide that just Windows would be fine let me know...

Xeel
August 5th, 2009, 01:40 PM
Windows (works for IE and Firefox):<script language="javascript" type="text/javascript">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

function loadXML(){
xmlDoc.async="false";
xmlDoc.load("xml_file.xml");
xmlObj=xmlDoc.documentElement;
var v = xmlObj.childNodes(0).getAttribute("id");
window.alert(v);
}
</script>

Firefox/Opera only method (cross-platform in theory):http://www.hiteshagrawal.com/javascript/javascript-xml-parsing-on-mozilla-firefox-opera-browsers

erevtsov
August 5th, 2009, 02:58 PM
Xeel I was able to open the XML file fine, I need help saving to a file.

PeejAvery
August 5th, 2009, 03:39 PM
Still, his post prior to that stands.

Since you need to run this cross-browser, you will have to use some form of server-side implementation (PHP, ASP.NET, JSP, etc.).

Xeel
August 5th, 2009, 04:02 PM
IE only:function write2File(txt)
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var file = fso.OpenTextFile("C:\\test.txt", 8, true,0);
file.write(txt);
file.close();
delete file;
delete fso;
}

erevtsov
August 6th, 2009, 09:46 AM
Maybe I should take a different approach. I have the parameters that I need saved in an array. How can I save this array to my XML file? (not using JavaScript)

PeejAvery
August 6th, 2009, 10:11 AM
You keep saying you need to save an XML file, but you still have yet to mention if the XML file is on the local host or accessed through a remote server.

Either way, any modern programming language has network frameworks which can read and write if the permissions are there. Why don't you work with something simple such as VB.NET?

erevtsov
August 6th, 2009, 10:22 AM
The XML file is on the local host, sorry I didn't mention it before. Thank you for all your help, Peej and Xeel. I just thought JavaScript would be the easiest to use, I wasn't aware of the permission limitations. I'm going to do this using Java now.