Read a xml file respecting its encoding information
npm install read-xml> Read a xml file respecting its encoding information
simple-iso-8859-1.xml
``xml`
Without this module the above xml file would be read incorrectly by the standard fs module, because node.js only supports some encodings in its core
`xml`
`js
'use strict';
var fs = require('fs'),
path = require('path'),
xmlReader = require('read-xml');
var FILE = path.join(__dirname, 'test/xml/simple-iso-8859-1.xml');
// pass a buffer or a path to a xml file
xmlReader.readXML(fs.readFileSync(FILE), function(err, data) {
if (err) {
console.error(err);
}
console.log('xml encoding:', data.encoding);
console.log('Decoded xml:', data.content);
});
`
`js
'use strict';
var fs = require('fs'),
path = require('path'),
xmlReader = require('read-xml');
var FILE = path.join(__dirname, 'test/xml/simple-iso-8859-1.xml');
var decodedXMLStream = fs.createReadStream(FILE).pipe(xmlReader.createStream());
decodedXMLStream.on('encodingDetected', function(encoding) {
console.log('Encoding:', encoding);
});
decodedXMLStream.on('data', function(xmlStr) {
console.log(xmlStr);
});
``
All encodings supported by iconv-lite