私はずっと探していて、いくつかの良い答えを思いつきましたが、私の仕事のこの最後の小さな詳細では、解決策を見つけることができませんでした.
次の関数を含む Node.js 実装があります。この関数のポイントは、サーバー上の xml ファイルの設定の一部を変更することです。これまでのところ、ほとんどすべてが完了していると思いますが、処理された xml 文字列をファイルに書き戻す方法がわかりません。
$data
POST からの JSON オブジェクトです。
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.
});
...
}
これは他の言語でも実行できることはわかっていますが、Node.js ソリューションが本当に必要です。また、jstoxml
モジュールとjquery
モジュールが実装にロードされています。