0

I have been searching all over and I have come up with some good answers, but this last small detail of my task, I have not been able to find a solution.

I have a Node.js implementation that contains the following function. The point of this function is to change some of the settings of an xml file on the server. So far I think I have pretty much everything done, but I can't figure out how to get the processed xml string to write back to the file.

$data is the JSON object from the POST.

function SaveSettings() {
    fs.readFile('config.xml', function (err, data) { //Read the XML file to a string
        if(err) {throw err;}    //If and error, throw error
        var $xml = $(data.toString()).find('settings').each(function () { //Process XML
            $(this).find('background > color').text($data.backgroundColor);
            $(this).find('background > image').text($data.backgroundImage);
            if($data.startupEnabled === "on") {
                $(this).find('startup > enabled').text("true");
            } else {
                $(this).find('startup > enabled').text("false");
            }
            if($data.splashEnabled === "on") {
                $(this).find('startup > splash > enabled').text("true");
            } else {
                $(this).find('startup > splash > enabled').text("false");
            }
            $(this).find('startup > splash > image').text($data.splashImage);
            $(this).find('security > password').text($data.password);
       });

       console.log($xml.toString()); //Contains the output of the jQuery object (Not XML)

        fs.writeFile('config.xml', data.toString(), function (err) {
            if(err) {throw err;}
        }); //data.toString() contains the original XML, unmodified.
    });

    ...
}

I know that this could be done with other languages, but I would really like a Node.js solution. Also I do have the jstoxml module and jquery module loaded in the implementation.

4
  • what part isn't actually working? You have an fs.writeFile command there, is it not getting called? Is it getting called but it's not writing? Help us help you =) Commented Jul 8, 2013 at 0:33
  • @Mike'Pomax'Kamermans yeah I thought I might have to be a little more clear about it, sorry about that. My problem is I don't know how to get the modified XML string from the jquery function. The fs.writeFile is working fine and is getting called, but the data.toString() still contains the original xml string, not the modified on. If I could just get the modified one, I could throw it in the fs.writeFile and I'd be done... Commented Jul 8, 2013 at 0:44
  • then you may want to change your post to ask how you can get the modified XML into a variable for writing, instead. I'd assume that putting your modified data into a throw-away div and then getting div.innerHTML would work just fine. Although looking at your code, what's $data? where is that even created? (If you're in Nodejs, you probably want to use a gruntfile with a js-hint task to catch rogue vars) Commented Jul 8, 2013 at 1:01
  • @Mike'Pomax'Kamermans I updated my question per your recommendations, thank you, and I added what $data is. It is basically the JSON containing the POST data with the values from the form the user is submitting. Commented Jul 8, 2013 at 1:22

1 Answer 1

1

You probably want to pass that $data into the functions, not rely on it being global. That said, you can probably get the XML code by doing:

var div = $("<div></div>");
div.append($(this));
var newXMLcode = div.html();

Also note that you're using $(this) all over the place - you probably want to cache that instead, like var $this = $(this) and then using $this instead for all your jQuery-wrapped requirements.

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

1 Comment

Perfect! Thank you! I took your advice and cached the $(this). Then I did all my selections from the root level and wrote everything back to the file. Works great now! I have been working on getting this to work for a day and a half. Finally I can move on to other development. I knew it had to be simple, but for the life of me I couldn't figure out how to pull the data... Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.