Node XML Stream Parser

1.0.12 · maintenance · verified Wed Apr 22

node-xml-stream-parser (current version 1.0.12) is a lightweight, fast XML/HTML stream parser designed for Node.js environments. It specializes in processing streaming XML, making it particularly suitable for tasks like parsing RSS, ATOM, or RDF feeds efficiently. The library's focus is on simplicity and performance, emitting events for `opentag`, `closetag`, `text`, `cdata`, and `instruction` as it processes the stream. It differentiates itself from more heavy-weight parsers (like `node-sax`) by offering a streamlined API and intentionally omitting support for less common XML features like comments, DOCTYPES, and ENTITY declarations, optimizing for speed and minimal overhead. It has seen consistent, though not rapid, updates, with the latest publish around 6 years ago on NPM, but GitHub activity suggests some more recent minor updates, making it a `maintenance` status.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate the XML stream parser, register event listeners for various XML node types (open/close tags, text, CDATA), handle errors, and detect the stream's completion. It includes examples for both directly writing XML strings and piping from a file stream.

import { createReadStream } from 'fs';
import Parser from 'node-xml-stream-parser';

const xmlString = '<root><item attr="value">Hello</item><![CDATA[Some Cdata]]><item/></root>';

const parser = new Parser();

parser.on('opentag', (name, attrs) => {
  console.log(`Open Tag: ${name}, Attributes:`, attrs);
});

parser.on('closetag', name => {
  console.log(`Close Tag: ${name}`);
});

parser.on('text', text => {
  if (text.trim() !== '') {
    console.log(`Text: '${text.trim()}'`);
  }
});

parser.on('cdata', cdata => {
  console.log(`CDATA: '${cdata}'`);
});

parser.on('error', err => {
  console.error('Parsing Error:', err.message);
});

parser.on('finish', () => {
  console.log('XML stream parsing finished.');
});

// Option 1: Write data directly
console.log('--- Parsing from string ---');
parser.write(xmlString);
parser.end();

// Option 2: Pipe a file stream (for larger files)
// Ensure 'feed.atom' exists with valid XML content for this part to run.
// For example, create a dummy feed.atom: <feed><entry><title>Test</title></entry></feed>
const filePath = './feed.atom';
console.log(`\n--- Piping from file: ${filePath} ---`);
// Create a dummy file for demonstration
import { writeFileSync } from 'fs';
writeFileSync(filePath, '<feed><entry><title>Hello World</title><content>Some content.</content></entry></feed>');

const fileStreamParser = new Parser();
fileStreamParser.on('opentag', (name) => console.log(`[File] Open Tag: ${name}`));
fileStreamParser.on('closetag', (name) => console.log(`[File] Close Tag: ${name}`));
fileStreamParser.on('text', (text) => { if (text.trim() !== '') console.log(`[File] Text: '${text.trim()}'`); });
fileStreamParser.on('error', (err) => console.error('[File] Error:', err.message));
fileStreamParser.on('finish', () => console.log('[File] Parsing from file finished.'));

createReadStream(filePath).pipe(fileStreamParser);

view raw JSON →