README.md
1 # xmlbuilder-js 2 3 An XML builder for [node.js](https://nodejs.org/) similar to 4 [java-xmlbuilder](https://github.com/jmurty/java-xmlbuilder). 5 6 [](http://opensource.org/licenses/MIT) 7 [](https://npmjs.com/package/xmlbuilder) 8 [](https://npmjs.com/package/xmlbuilder) 9 10 [](http://travis-ci.org/oozcitak/xmlbuilder-js) 11 [](https://david-dm.org/oozcitak/xmlbuilder-js) 12 [](https://coveralls.io/github/oozcitak/xmlbuilder-js) 13 14 ### Installation: 15 16 ``` sh 17 npm install xmlbuilder 18 ``` 19 20 ### Usage: 21 22 ``` js 23 var builder = require('xmlbuilder'); 24 var xml = builder.create('root') 25 .ele('xmlbuilder') 26 .ele('repo', {'type': 'git'}, 'git://github.com/oozcitak/xmlbuilder-js.git') 27 .end({ pretty: true}); 28 29 console.log(xml); 30 ``` 31 32 will result in: 33 34 ``` xml 35 <?xml version="1.0"?> 36 <root> 37 <xmlbuilder> 38 <repo type="git">git://github.com/oozcitak/xmlbuilder-js.git</repo> 39 </xmlbuilder> 40 </root> 41 ``` 42 43 It is also possible to convert objects into nodes: 44 45 ``` js 46 builder.create({ 47 root: { 48 xmlbuilder: { 49 repo: { 50 '@type': 'git', // attributes start with @ 51 '#text': 'git://github.com/oozcitak/xmlbuilder-js.git' // text node 52 } 53 } 54 } 55 }); 56 ``` 57 58 If you need to do some processing: 59 60 ``` js 61 var root = builder.create('squares'); 62 root.com('f(x) = x^2'); 63 for(var i = 1; i <= 5; i++) 64 { 65 var item = root.ele('data'); 66 item.att('x', i); 67 item.att('y', i * i); 68 } 69 ``` 70 71 This will result in: 72 73 ``` xml 74 <?xml version="1.0"?> 75 <squares> 76 <!-- f(x) = x^2 --> 77 <data x="1" y="1"/> 78 <data x="2" y="4"/> 79 <data x="3" y="9"/> 80 <data x="4" y="16"/> 81 <data x="5" y="25"/> 82 </squares> 83 ``` 84 85 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.