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.
npm install saxVerified import paths — ran on the pinned version, not inferred.
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.
Carefully choose the `strict` option based on your XML's compliance level. For parsing HTML or less rigidly formed XML, `strict: false` is often necessary. Always ensure attribute values are quoted if `strict: true`.
For documents with custom DTD entities, implement a listener for the `ondoctype` event to parse the DTD, extract entities, and populate `parser.ENTITIES` manually. Otherwise, ensure input XML relies only on standard entities or process unknown entities in loose mode (`strict: false`).
If you require robust HTML parsing with error correction or a DOM structure, use a dedicated HTML parser like `parse5` or `jsdom`. If you need to build a DOM from XML, you must manually construct it using the events emitted by the `sax` parser.
Always include an `on('error', ...)` handler for `saxStream` instances. Within this handler, ensure you clear `this._parser.error = null;` and call `this._parser.resume();` if you intend for parsing to recover and continue after non-fatal errors.Either set `strict: false` in the parser options, or manually define the entity by listening to `ondoctype` and adding it to `parser.ENTITIES`.
Ensure the XML document adheres to strict XML rules, having a single root element and all text nodes properly enclosed within tags. Use `strict: false` if dealing with less rigid XML or HTML-like content.
Ensure all attribute values are properly quoted (e.g., `<tag foo="bar">` or `<tag foo='bar'>`). If dealing with XML that intentionally uses unquoted attributes, set `strict: false` and ensure `unquotedAttributeValues: true` in the parser options.
No dependency data recorded yet.