Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including read-xml with all npm packages installed. Try it out:

var readXml = require("read-xml")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

read-xml v3.0.0

Read a xml file respecting its encoding information

read-xml

NPM Version License Build Status

Read a xml file respecting its encoding information

Usage

simple-iso-8859-1.xml

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<test>
  <value>ácentó y la letra ñ<value>
</test>

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

<!-- output produced by fs.readFile/fs.readFileSync -->
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<test>
  <value>�cent� y la letra �<value>
</test>

Basic API

'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);
});

Streaming API

'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);
});

Supported encodings

All encodings supported by iconv-lite

License

See license

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free