Native and full Javascript implementation of the classic XMLWriter class
npm install xml-writer
It's native and full javascript implementation of the classic XMLWriter class.
The API is complete, flexible and tolerant.
XML is still valid.
* Nicolas Thouvenin
* Anton Zem
* Chip Lee
* Peecky
* Julian Scheid
* Guillaume Plique
With npm do:
$ npm install xml-writer
javascript
var XMLWriter = require('xml-writer');
xw = new XMLWriter;
xw.startDocument();
xw.startElement('root');
xw.writeAttribute('foo', 'value');
xw.text('Some content');
xw.endDocument(); console.log(xw.toString());
`
Output:
Some content
Tip: If you want your XML indented use
new XMLWriter(true) or new XMLWriter('\t'), for instance, if you want to use a custom string for indentation.Chaining
`javascript
var XMLWriter = require('xml-writer');
xw = new XMLWriter;
xw.startDocument().startElement('root').writeAttribute('foo', 'value').writeElement('tag', 'Some content'); console.log(xw.toString());
`
Output:
Some content
Tolerant
`javascript
var XMLWriter = require('xml-writer');
xw = new XMLWriter;
xw.startElement('root').writeAttribute('foo', 'value').text('Some content'); console.log(xw.toString());
`
Output: Some content
Extensible
`javascript
var XMLWriter = require('xml-writer'),
fs = require('fs');
var ws = fs.createWriteStream('/tmp/foo.xml');
ws.on('close', function() {
console.log(fs.readFileSync('/tmp/foo.xml', 'UTF-8'));
});
xw = new XMLWriter(false, function(string, encoding) {
ws.write(string, encoding);
});
xw.startDocument('1.0', 'UTF-8').startElement(function() {
return 'root';
}).text(function() {
return 'Some content';
});
ws.end();
``Output:
Use nodeunit to run the tests.
$ npm install nodeunit
$ nodeunit test
* https://github.com/minchenkov/simple-xml-writer
* https://github.com/wezm/node-genx
