javascript - How to add some extra data to an existing file using node js -


i have file this

<?xml version="1.0" encoding="utf-8"?> <quiz> <title>matching</title>  /* rewrite */ <questions> <question>can passed on environment individual. can person, insects or physical environment. </question> <answer>communicable</answer> </questions> /* rewrite end */  </quiz> 

now want add data before </quiz> tag this:

 <?xml version="1.0" encoding="utf-8"?> <quiz> <title>matching</title>   <questions> <question>can passed on environment individual. can person, insects or physical environment. </question> <answer>communicable</answer> </questions>  <questions> <question>some txt</question> <answer>some txt</answer> </questions>   </quiz> 

i using

fs.writefile("templatexml.xml", data["message"] , function(err) {   if(err) {     console.log(err);   } else {     console.log("the file saved!");   } }); 

it rewrites file every time want read file , and write content on file how can this?

i have kind of hacky solution problem.. format xml file this:

<?xml version="1.0" encoding="utf-8"?> <quiz> <title>matching</title> /* rewrite */ <questions> <question>can passed on environment individual. can    person, insects or physical environment. </question> <answer>communicable</answer> </questions> //cursor </quiz> 

and code 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");     }); }); 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -