You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.5 KiB

4 years ago
  1. # xmlbuilder-js
  2. An XML builder for [node.js](https://nodejs.org/) similar to
  3. [java-xmlbuilder](https://github.com/jmurty/java-xmlbuilder).
  4. [![License](http://img.shields.io/npm/l/xmlbuilder.svg?style=flat-square)](http://opensource.org/licenses/MIT)
  5. [![NPM Version](http://img.shields.io/npm/v/xmlbuilder.svg?style=flat-square)](https://npmjs.com/package/xmlbuilder)
  6. [![NPM Downloads](https://img.shields.io/npm/dm/xmlbuilder.svg?style=flat-square)](https://npmjs.com/package/xmlbuilder)
  7. [![Travis Build Status](http://img.shields.io/travis/oozcitak/xmlbuilder-js.svg?style=flat-square)](http://travis-ci.org/oozcitak/xmlbuilder-js)
  8. [![AppVeyor Build status](https://ci.appveyor.com/api/projects/status/bf7odb20hj77isry?svg=true)](https://ci.appveyor.com/project/oozcitak/xmlbuilder-js)
  9. [![Dev Dependency Status](http://img.shields.io/david/dev/oozcitak/xmlbuilder-js.svg?style=flat-square)](https://david-dm.org/oozcitak/xmlbuilder-js)
  10. [![Code Coverage](https://img.shields.io/coveralls/oozcitak/xmlbuilder-js.svg?style=flat-square)](https://coveralls.io/github/oozcitak/xmlbuilder-js)
  11. ### Installation:
  12. ``` sh
  13. npm install xmlbuilder
  14. ```
  15. ### Usage:
  16. ``` js
  17. var builder = require('xmlbuilder');
  18. var xml = builder.create('root')
  19. .ele('xmlbuilder')
  20. .ele('repo', {'type': 'git'}, 'git://github.com/oozcitak/xmlbuilder-js.git')
  21. .end({ pretty: true});
  22. console.log(xml);
  23. ```
  24. will result in:
  25. ``` xml
  26. <?xml version="1.0"?>
  27. <root>
  28. <xmlbuilder>
  29. <repo type="git">git://github.com/oozcitak/xmlbuilder-js.git</repo>
  30. </xmlbuilder>
  31. </root>
  32. ```
  33. It is also possible to convert objects into nodes:
  34. ``` js
  35. builder.create({
  36. root: {
  37. xmlbuilder: {
  38. repo: {
  39. '@type': 'git', // attributes start with @
  40. '#text': 'git://github.com/oozcitak/xmlbuilder-js.git' // text node
  41. }
  42. }
  43. }
  44. });
  45. ```
  46. If you need to do some processing:
  47. ``` js
  48. var root = builder.create('squares');
  49. root.com('f(x) = x^2');
  50. for(var i = 1; i <= 5; i++)
  51. {
  52. var item = root.ele('data');
  53. item.att('x', i);
  54. item.att('y', i * i);
  55. }
  56. ```
  57. This will result in:
  58. ``` xml
  59. <?xml version="1.0"?>
  60. <squares>
  61. <!-- f(x) = x^2 -->
  62. <data x="1" y="1"/>
  63. <data x="2" y="4"/>
  64. <data x="3" y="9"/>
  65. <data x="4" y="16"/>
  66. <data x="5" y="25"/>
  67. </squares>
  68. ```
  69. See the [wiki](https://github.com/oozcitak/xmlbuilder-js/wiki) for details and [examples](https://github.com/oozcitak/xmlbuilder-js/wiki/Examples) for more complex examples.