SAX Streaming XML Parser

1.6.0 · maintenance · verified Sun Apr 19

sax is an evented streaming XML parser implemented in JavaScript, designed primarily for Node.js but also functional in browser environments and other CommonJS implementations. It provides a SAX-style API, emitting events for different XML constructs such as `ontext`, `onopentag`, and `onattribute` as it processes input. The current stable version, as specified, is 1.6.0, indicating a mature and stable API, although new feature releases are infrequent. Key differentiators include its lightweight nature, efficient streaming capabilities, and its explicit focus on parsing XML rather than attempting to correct malformed HTML. It avoids the complexities associated with full DOM construction, XSLT transformations, or comprehensive schema/DTD validation, making it suitable for scenarios requiring simple, fast XML event processing. It offers both a direct parser interface for string input and a Node.js stream API for handling larger files efficiently.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates both direct parser usage with event listeners for string input and stream-based parsing, including error handling and common configuration options for processing XML.

const sax = require('sax');
const stream = require('stream');

// --- Direct Parser Example ---
const strictMode = true; // set to false for html-mode
const directParser = sax.parser(strictMode);

directParser.onerror = function (e) {
  console.error("Direct Parser Error:", e.message);
  if (!strictMode) {
    // In loose mode, clear error and resume to try and continue parsing
    this._parser.error = null;
    this._parser.resume();
  }
};
directParser.ontext = function (t) {
  const trimmedText = t.trim();
  if (trimmedText) console.log("Direct Text:", trimmedText);
};
directParser.onopentag = function (node) {
  console.log("Direct Open Tag:", node.name, "Attributes:", JSON.stringify(node.attributes));
};
directParser.onclosetag = function () {
  console.log("Direct Close Tag");
};
directParser.onend = function () {
  console.log("Direct Parser End.\n");
};

console.log("--- Parsing XML directly ---");
directParser.write('<root><data name="example">Hello</data>, <world/></root>').close();


// --- Stream Parser Example ---
const streamMode = false; // loose mode for more forgiving parsing
const saxStream = sax.createStream(streamMode, {
  trim: true,
  normalize: true,
  lowercase: true
});

saxStream.on('error', function (e) {
  console.error('Stream Error:', e.message);
  // Crucial for stream to continue if you want to recover after non-fatal errors
  this._parser.error = null;
  this._parser.resume();
});

saxStream.on('opentag', function (node) {
  console.log('Stream Open Tag:', node.name, JSON.stringify(node.attributes));
});

saxStream.on('text', function (t) {
  const trimmedText = t.trim();
  if (trimmedText) console.log('Stream Text:', trimmedText);
});

saxStream.on('end', function () {
  console.log('Stream End.');
});

console.log("--- Parsing XML via stream ---");
// Simulate a readable stream from a string buffer
const xmlContent = Buffer.from('<catalog><book id="1"><title>The Great Book</title></book><book id="2"></book></catalog>');
const readableStream = stream.Readable.from(xmlContent);

readableStream.pipe(saxStream);

view raw JSON →