1

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="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>
        An Ionic Framework and 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 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 ..?

2 Answers 2

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);

    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

can i get widget version ..?
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.