This library will convert a XML from a buffer to a string, dealing with the different encodings that can be used when serializing a XML.
Currently it fully supports UTF-8, UTF-16 and the ISO-8859 family.
npm i xml-buffer-tostring
The library exports a function that can be used for the conversion:
const xmlBufferToString = require('xml-buffer-tostring');
const xmlString = xmlBufferToString(buffer);
When it is not possible to extract the encoding then the default encoding is used.
You can pass a default encoding using the options parameter. Since this library uses iconv-lite
internally you can choose one from their supported encodings. When no default encoding is passed utf8
is used.
const xmlBufferToString = require('xml-buffer-tostring');
const xmlString = xmlBufferToString(buffer, { defaultEncoding: 'ISO-8859-1' });
Although the library uses encoding detection with and without BOM following the indications of the specification, it is not strict with the failures, instead it uses precedence.
If an encoding attribute is found on the xml declaration it will use that as encoding, otherwise the detected encoding (from BOM or <?
chars) would be used.
The MIT License. See LICENSE file.