Registry / serialization / node-xml-stream-parser

node-xml-stream-parser

JSON →
library1.0.12jsnpmunverified

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.

npm install node-xml-stream-parser
INSTALL
IMPORT
SIG · NODE-XML-STREAM-PA
N
node-xml-stream-parser
serializationjavascriptv1.0.12
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Parser
import Parser from 'node-xml-stream-parser';
const Parser = require('node-xml-stream');
The README example shows `require('node-xml-stream')`, but the correct package name for installation is `node-xml-stream-parser`. While it primarily uses CommonJS `require`, modern Node.js applications might attempt ESM `import`. It's generally safe to assume that a package primarily designed for CommonJS will work with default ESM imports if Node.js's module interop is enabled, but explicit CommonJS is shown in the quickstart.
parser.on
parser.on('opentag', (name, attrs) => { /* ... */ });
parser.addListener('opentag', handler);
The primary interaction model is event-driven using `.on()`, similar to Node.js's EventEmitter, which the parser extends. While `addListener` technically works, `on` is the idiomatic choice.

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);
Debug
Known issues
gotchaThe package name for `require` in the README example (`node-xml-stream`) is incorrect. The actual npm package to install and import is `node-xml-stream-parser`.
fix
Always use `require('node-xml-stream-parser')` or `import Parser from 'node-xml-stream-parser'`.
affects: >=1.0.0
gotchaThis parser is designed to be lightweight and fast, intentionally ignoring comments (`<!-- -->`), `<!DOCTYPES>`, and `<!ENTITY>` declarations. If your application relies on parsing these XML features, this library will not extract them.
fix
If full XML feature support is required, consider alternative parsers like `node-sax` or `xml2js` which offer more comprehensive XML specification compliance.
affects: >=1.0.0
gotchaSelf-closing tags that do not have an empty space before the closing slash (e.g., `<tag/>` instead of `<tag />`) may behave unexpectedly if the tag name itself ends with a `/`. The parser strips the last `/` character from the tag name in such cases, which can lead to misinterpretation if your tag is genuinely named something ending with `/`.
fix
Ensure all self-closing tags conform to the standard `<tag />` format with a space before the slash, or avoid tag names ending with `/` if using this parser.
affects: >=1.0.0
maintenanceThis project is a fork of an inactive `node-xml-stream` project, as noted in its README. While this fork has seen some updates, the `last publish` date on npm for version 1.0.12 is 6 years ago (February 10, 2020). While the GitHub repository may show more recent activity, be aware that official releases are infrequent.
fix
For critical projects requiring active maintenance and frequent updates, assess the current GitHub activity or consider more actively maintained XML parsing libraries.
affects: <=1.0.12
Errors
Common errors & fixes
Error: Cannot find module 'node-xml-stream'
Attempting to `require()` or `import` the package using the incorrect name specified in the README's usage example, instead of the actual npm package name.
fix
Change the import statement to `require('node-xml-stream-parser')` for CommonJS or `import Parser from 'node-xml-stream-parser'` for ESM.
TypeError: parser.on is not a function
The `Parser` class itself is being called like a function, or `new Parser()` was not used, resulting in an undefined or improperly initialized object that does not expose the event emitter methods.
fix
Ensure you instantiate the parser correctly with `let parser = new Parser();` before attempting to attach event listeners.
Events like 'instruction' or 'cdata' are not firing for valid XML that contains them.
This is typically not an error but a misunderstanding of which events are emitted by the parser. While the parser supports these events, they only fire if the input XML stream actually contains the corresponding elements (e.g., `<?xml ...?>` for 'instruction' or `<![CDATA[...]]>` for 'cdata').
fix
Verify that your XML input correctly includes the specific XML constructs you expect to trigger these events. Also, double-check event handler registration.
Upgrade
Version history
1.0.12latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
node-xml-stream-parser — npm install node-xml-stream-parser · libregistry