如何从xml文件中读取值并将其添加到json文件中?

时间:2021-06-22 23:09:11

I'm a bit of a noob to Gulp/Node programming and I'm struggling to do a simple task :)

我有点难以接受/节点编程,很难完成一个简单的任务:

I have an XML file which contains a node like this: <widget version="1.1"></widget>

我有一个包含如下节点的XML文件:

I would like to read out the version value and put that value into a json file. How is this achieved?

我想读出版本值并将该值放入json文件中。这是如何做到的呢?

Here's what I tried:

这就是我试着:

var configXml      = 'config.xml';
var enTemplateJson = 'signing/en_template.json';

CODE:

代码:

SCRIPT:

脚本:

return gulp.src([configXml])
          .pipe(cheerio({
            parserOptions: {
              xmlMode: true
            },
            run: function($, file) {
              $('widget').each(function() {
                var version = $(this)[0].attribs.version;

                // prints version 1.1
                console.log(version);
              })
            }
          }))
          .pipe(gulp.dest('test.xml'));

1 个解决方案

#1


3  

I figured it out:

我想通了:

// App Versioning
gulp.task('update-version', function() {
  // Read config.xml file synchronously
  var xml       = fs.readFileSync('./config.xml');

  // Use cheerio to parse the xml and extract the version number
  var $         = cheerio.load(xml, { xmlMode: true });
  var version   = $('widget')[0].attribs.version;

  return gulp.src('signing/en.json')
          .pipe(replace(/\${APP_VERSION}/, version))
          .pipe(gulp.dest('app/i18n'));
});

#1


3  

I figured it out:

我想通了:

// App Versioning
gulp.task('update-version', function() {
  // Read config.xml file synchronously
  var xml       = fs.readFileSync('./config.xml');

  // Use cheerio to parse the xml and extract the version number
  var $         = cheerio.load(xml, { xmlMode: true });
  var version   = $('widget')[0].attribs.version;

  return gulp.src('signing/en.json')
          .pipe(replace(/\${APP_VERSION}/, version))
          .pipe(gulp.dest('app/i18n'));
});