0

私はずっと探していて、いくつかの良い答えを思いつきましたが、私の仕事のこの最後の小さな詳細では、解決策を見つけることができませんでした.

次の関数を含む Node.js 実装があります。この関数のポイントは、サーバー上の xml ファイルの設定の一部を変更することです。これまでのところ、ほとんどすべてが完了していると思いますが、処理された xml 文字列をファイルに書き戻す方法がわかりません。

$dataPOST からの 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モジュールが実装にロードされています。

4

1 に答える 1

1

$dataグローバルであることに依存するのではなく、おそらくそれを関数に渡したいと思うでしょう。そうは言っても、おそらく次のようにして XML コードを取得できます。

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

また、あちこちで使用していることにも注意してください。$(this)おそらく、代わりにそれをキャッシュして、jQuery でラップされたすべての要件に代わりにvar $this = $(this)使用することをお勧めします。$this

于 2013-07-08T02:15:25.427 に答える