Skip to content Skip to sidebar Skip to footer

How To Add Some Extra Data To An Existing File Using Node Js

I have a file like This Matching /* Rewrite */ Can be passed

Solution 1:

I have a kind of hacky solution for your problem.. format you xml file like this:

<?xml version="1.0" encoding="utf-8"?><Quiz><title>Matching</title>
/* Rewrite */
<questions><question>Can be passed on from the environment to the individual. This can be from    person, insects or from the physical environment. </question><answer>Communicable</answer></questions>
//cursor
</quiz>

and the code for appending new data:

var fs = require("fs");
var addData = "<questions><question>Some Txt</question><answer>Some Txt</answer>     </questions>";
var cursor = "//cursor";
addData += cursor;
fs.readFile("Templatexml.xml", "utf-8",function(err,data) {
    if(err) {
        console.log(err);
        return;
     }
    var newData = data.replace(/\/\/cursor/,addData);
    fs.writeFile("Templatexml.xml", newData , function(err) {
    if(err) {
            console.log(err);
            return;
     } 
     console.log("done");
    });
});

Post a Comment for "How To Add Some Extra Data To An Existing File Using Node Js"