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.
fs.writeFilecommand there, is it not getting called? Is it getting called but it's not writing? Help us help you =)fs.writeFileis working fine and is getting called, but thedata.toString()still contains the original xml string, not the modified on. If I could just get the modified one, I could throw it in thefs.writeFileand I'd be done...divand then gettingdiv.innerHTMLwould 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)$datais. It is basically the JSON containing the POST data with the values from the form the user is submitting.