node.js - adding new element to the xml using node js -
var readline = require('readline'); var et = require('elementtree'); var xml = et.xml; var elementtree = et.elementtree; var element = et.element; var subelement = et.subelement; var fs = require('fs'); var root; var datanew, etreenew; var etree; var rl = readline.createinterface ({ input: process.stdin, output: process.stdout }); function createxml(root) { etree = new elementtree(root); var done = json.stringify(etree); console.log(done); rl.question('\nenter file name create xml: \n', function (d){ var xml = root.write({'xml_declaration': false}); console.log(xml); var newfile = fs.write(d,xml); console.log("\nxml file updated..!!\n"); }); } function getelements(subroot, numelements) { if(numelements > 0) { rl.question('\nenter element name: \n', function(name1) { rl.question('\nenter element data: \n', function (data) { var subroot2 = subelement(subroot, name1); subroot2.text = data; numelements = numelements - 1; getelements(subroot, numelements); }); }); }else { createxml(root); } } function start() { rl.question('\nenter file name update xml: \n', function (f){ datanew = fs.readfilesync(f).tostring(); root = et.parse(datanew); rl.question('\nenter sub root element name: \n', function (subrootname) { var find1 = root.find(subrootname); var subroot = subelement(find1); rl.question('\nenter number of elements: \n', function(numelements) { getelements(subroot, numelements); }); }); }); } start();
above script using modify xml file using node js getting error while writing updated xml file. 'error: invalid tag type serialization: undefined'. done wrong here?
Comments
Post a Comment