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 1,000,000+ packages pre-installed, including node-xml-lite2 with all npm packages installed. Try it out:

var nodeXmlLite2 = require("node-xml-lite2")

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

node-xml-lite2 v0.1.2

Pure javascript XML ANSI/Unicode SAX parser for Node.js

node-xml-lite2

A stripped down fork of https://github.com/hgourvest/node-xml-lite

  • This is a pure javascript XML SAX parser for Node.js.
  • The specificity of this xml parser is that it can parse a document from a Buffer.
  • It relies on iconv-lite to decode the text according to the code page of the document.

Differences from node-xml-lite

  • Removed all SAXParse* API variants until I have time to rewrite them into a parseStream API
  • Exported the raw XmlParser class to allow for custom, lightweight parsing jobs (somewhat making up for the removal of the SAXParse* APIs)
  • Added TypeScript definition file
  • Added an offset property to the parser to complement line + col

Installation

npm install node-xml-lite2

Sample usage

Parse a file

var xml = require('node-xml-lite2');
xml.parseFile('~/test.xml', function(err, root) {
  // ...
});

Parse a file synchronously

var xml = require('node-xml-lite2');
var root = xml.parseFileSync('~/test.xml');

Parse a string

var xml = require('node-xml-lite2');
xml.parseString('<xml>hello</xml>');

Parse a buffer

var xml = require('node-xml-lite2');
xml.parseBuffer(Buffer.from('<xml>hello</xml>', 'utf8'));

Advanced usage

Custom parsing

var xml = require('node-xml-lite2');
var parser = new xml.XmlParser();
var buffer = fs.readFileSync('~/large-file.xml');

parser.parseBuffer(buffer, buffer.length, function(state, name, value) {
  switch (state) {
    case xml.xtOpen:
      console.log('opening:', name);
      break;
    case xml.xtClose:
      console.log('closing:', name);
      break;
    case xml.xtAttribute:
      console.log('attribute:', name + '=' + value);
      break;
    case xml.xtCData:
      console.log('CDATA:', name);
      break;
    case xml.xtComment:
      console.log('comment:', name);
      break;
  }

  return true;
});
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