Node.js: How to create XML files

前端 未结 2 837
长情又很酷
长情又很酷 2021-02-03 18:04

Is there a good way to create XML files? For example, like the Builder for Rails (or any other way)?

Thanks

2条回答
  •  粉色の甜心
    2021-02-03 18:37

    It looks like the xmlbuilder-js library may do this for you. If you have npm installed, you can npm install xmlbuilder.

    It will let you do this (taken from their example):

    var builder = require('xmlbuilder');
    var doc = builder.create('root');
    
    doc.ele('xmlbuilder')
        .att('for', 'node-js')
        .ele('repo')
          .att('type', 'git')
          .txt('git://github.com/oozcitak/xmlbuilder-js.git') 
        .up()
      .up()
      .ele('test')
        .txt('complete');
    
    console.log(doc.toString({ pretty: true }));
    

    which will result in:

    
      
        git://github.com/oozcitak/xmlbuilder-js.git
      
      complete
    
    

提交回复
热议问题