Modify XML tags on "NodeJS"

Does anyone know how to modify a tag value of an XML file using NodeJS

this is my XML file:

<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>

I want to change the <node> to <n>

I'm running nodeJS on windows. SO far I have following code;

var parser = new xml2js.Parser();
function xmltxt(response, postData){
    fs.readFile( './foo.xml', function(err, data) {
        parser.parseString(data, function (err, result) {
            console.dir(result.note.to[0]);
        });
    });
}

This reads the node value but I need to modify the tag value. Please help.

Using damn-simple-xml you can do the following:

var Serializer = require("damn-simple-xml");  // constructor
var dsx = new Serializer();
var fs = require("fs");

var out = fs.createWriteStream("./foo2.xml");

dsx.deserialize(fs.createReadStream("./foo.xml"), function(err, root) {
    if (err) {
        console.log(err);
        return;
    }
    dsx.serialize({
        name: "n",
        data: root.data
    }, 
    function(err, xmlpart, level) {
        if (err) {
            console.log(err);
            return;
        }
        out.write(xmlpart);
        if (level === 0) { // XML streaming done
            out.end(); // closes the stream.
        }
    });
});

In the previous example, we receive a root object in the deserialization callback. This object consist of root.name (i.e. the name of the root node) and root.data which is the actual data from the deserialized XML document.

Then when serializing, we give back the same root.data object but there we change the root node's name to "n" as specified in your question.

Since damn-simple-xml can use streams to input xml and outputs it's content as XML chunks, the library have a very small memory footprint.