I have a libxmljs XML object. I want to write it to a file; here is so far what I have.
var libxml = require('libxmljs');
var xml = '<?xml version="1.0" encoding="UTF-8"?>' +
'<root>' +
'<child foo="bar">' +
'<grandchild baz="fizbuzz">'+
'<blah>grandchild content<inblah>blah blah</inblah></blah>' +
'<blah1>grandchild content blah2</blah1>'+
'</grandchild>' +
'</child>' +
'</root>';
var xmlDoc = libxml.parseXml(xml);
//..... do some changes to xmlDoc
console.log(xmlDoc.toString());
I want to write xmlDoc to a separate file. Such as result.xml
Use the built in fs module in node.js. Like this:
var js = require('fs');
var libxml = require('libxmljs');
var xml = '<?xml version="1.0" encoding="UTF-8"?><root>foobar</root>';
var xmlDoc = libxml.parseXml(xml);
//..... do some changes to xmlDoc
fs.writeFile('test.xml', xmlDoc.toString(), function(err) {
if (err) throw err;
console.log('Wrote XML string to test.xml');
});
You can read further about the fs module here: http://nodejs.org/api/fs.html#fs_fs_readfile_filename_options_callback