CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Oct 2008
    Posts
    25

    Opening and saving a XML file.

    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.

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Opening and saving a XML file.

    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.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Oct 2008
    Posts
    25

    Re: Opening and saving a XML file.

    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?

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: Opening and saving a XML file.

    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.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Oct 2008
    Posts
    25

    Re: Opening and saving a XML file.

    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?

  6. #6
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    Re: Opening and saving a XML file.

    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...
    Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?

    I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)

    //always looking for job opportunities in AU/NZ/US/CA/Europe :P
    willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));

    USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!

  7. #7
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    Re: Opening and saving a XML file.

    Windows (works for IE and Firefox):
    Code:
    <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/javascr...opera-browsers
    Last edited by Xeel; August 5th, 2009 at 01:45 PM.
    Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?

    I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)

    //always looking for job opportunities in AU/NZ/US/CA/Europe :P
    willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));

    USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!

  8. #8
    Join Date
    Oct 2008
    Posts
    25

    Re: Opening and saving a XML file.

    Xeel I was able to open the XML file fine, I need help saving to a file.

  9. #9
    Join Date
    May 2002
    Posts
    10,943

    Re: Opening and saving a XML file.

    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.).
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  10. #10
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    Re: Opening and saving a XML file.

    IE only:
    Code:
    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;
    }
    Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?

    I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)

    //always looking for job opportunities in AU/NZ/US/CA/Europe :P
    willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));

    USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!

  11. #11
    Join Date
    Oct 2008
    Posts
    25

    Re: Opening and saving a XML file.

    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)
    Last edited by erevtsov; August 6th, 2009 at 09:54 AM.

  12. #12
    Join Date
    May 2002
    Posts
    10,943

    Re: Opening and saving a XML file.

    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?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  13. #13
    Join Date
    Oct 2008
    Posts
    25

    Re: Opening and saving a XML file.

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured