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

I want to read specific xml tag and update it.here is the xml file<?xml version="1.0" enco

I want to read specific xml tag and update it.

here is the xml file

  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <widget id=".ionicframework.myapp450442" version="0.0.1" xmlns="" xmlns:cdv=".0">
   <name>myApp</name>
    <description>
        An Ionic Framework and Cordova project.
    </description>
    <author email="hi@ionicframework" href="/">
      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 this similar question on stack over flow but it did n't work for me.

here is the code which i 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 somebody tell me what might be the issue here?

is there any better npm module ..?

I want to read specific xml tag and update it.

here is the xml file

  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <widget id=".ionicframework.myapp450442" version="0.0.1" xmlns="http://www.w3/ns/widgets" xmlns:cdv="http://cordova.apache/ns/1.0">
   <name>myApp</name>
    <description>
        An Ionic Framework and Cordova project.
    </description>
    <author email="hi@ionicframework" href="http://ionicframework./">
      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 this similar question on stack over flow but it did n't work for me.

here is the code which i 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 somebody tell me what might be the issue here?

is there any better npm module ..?

Share Improve this question edited May 23, 2017 at 12:17 CommunityBot 11 silver badge asked Feb 3, 2016 at 4:56 ThusithzThusithz 8841 gold badge14 silver badges35 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You can parse XML to JSON, replace the need, and save back to XML.

For example, look at 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);

    });
});

small changes in stdob's answer

var fs = require('fs'),
    xml2js = require('xml2js');

var parser = new xml2js.Parser(),
    xmlBuilder = new xml2js.Builder();

fs.readFile(configFile, function(err, data) {
    parser.parseString(data, function (err, result) {

        console.log(result)        

        result.widget.name = ['new name'];

        var xml = xmlBuilder.buildObject(result);

        fs.writeFile(configFile, xml);

    });
});

he used unnecessary module.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744898902a4599859.html

相关推荐

  • javascript - Update xml file using fs-extra in Node JS - Stack Overflow

    I want to read specific xml tag and update it.here is the xml file<?xml version="1.0" enco

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信