javascript - Update xml file using fs-extra in Node JS -


i want read specific xml tag , update it.

here xml file

  <?xml version="1.0" encoding="utf-8" standalone="yes"?>   <widget id="com.ionicframework.myapp450442" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">    <name>myapp</name>     <description>         ionic framework , cordova project.     </description>     <author email="hi@ionicframework" href="http://ionicframework.com/">       ionic framework team     </author>     <content src="index.html"/>     <access origin="*"/>     <preference name="webviewbounce" value="false"/>     <preference name="uiwebviewbounce" value="false"/>     <preference name="disallowoverscroll" value="true"/>     <preference name="android-minsdkversion" value="16"/>     <preference name="backupwebstorage" value="none"/>     <feature name="statusbar">      <param name="ios-package" value="cdvstatusbar" onload="true"/>     </feature>   </widget> 

i followed similar question on stack on flow did n't work me.

here code used

   fs.readfile(configfile, 'utf-8',function(err, data) {       if (err) {          return console.log(err);       }        var name="<name>"+appname+"</name>";       var cursor = "//cursor";       var result = data.replace(/\/\/cursor/,name);        fs.writefile(configfile, result, 'utf-8', function(err) {           if (err) return console.log(err);       });       fs.writefile(appiconfile, icon, 'base64', function(err) {           if (err) return console.log(err);       });   }); 

can tell me might issue here?

is there better npm module ..?

you can parse xml json, replace need, , save xml.

for example, @ xml2js module:

var fs = require('fs'),     xml2js = require('xml2js'),     util = require('util');  var parser = new xml2js.parser(),     xmlbuilder = new xml2js.builder();  fs.readfile(configfile, function(err, data) {     parser.parsestring(data, function (err, result) {          console.log(util.inspect(result, false, null))                  result.widget.name = ['new name'];          var xml = xmlbuilder.buildobject(result);          fs.writefile(configfile, xml);      }); }); 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

python - cx_oracle unable to find Oracle Client -