Saxes is an evented, streaming XML parser for JavaScript, currently at version 6.0.0. It is a modern, stricter, and significantly faster fork of the original `sax` library, designed primarily for Node.js environments (requiring Node.js >=12.22.7) but also functional in browsers. Its core differentiator is a strong adherence to XML 1.0/1.1 and Namespaces in XML 1.0/1.1 well-formedness rules, unlike `sax` which tolerates malformed structures. This makes `saxes` unsuitable for HTML or pseudo-XML parsing, as it will explicitly report well-formedness errors. While it is a non-validating parser, it aims to catch all malformed constructs outside of thorough DTD validation. `saxes` does not include a `Stream` API, a notable departure from its `sax` predecessor, and its `onerror` handler defaults to throwing errors, which can be overridden. The project is actively maintained, with a focus on performance and strict conformance to XML specifications.
npm install saxesVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic parsing of an XML string, handling open tags, text content, close tags, and errors using event listeners. It shows both attributes and CDATA sections.
Ensure all input XML strictly adheres to XML 1.0/1.1 and Namespaces specifications. Do not feed HTML or pseudo-XML to saxes.
Replace `Stream` usage with direct calls to `parser.write(chunk)` and `parser.close()` for input termination.
Implement a custom `parser.on('error', handlerFunction)` to catch and handle parsing errors gracefully. If your handler does nothing, there is no `resume` method to call.For DTD-defined entities, you must manually listen to the `doctype` event, fetch the DTD content, parse it, and add custom entities to `parser.ENTITIES` if you require their resolution.
Ensure your runtime environment meets the minimum Node.js requirement of v12.22.7 or a modern browser environment. Transpilation to ES5 is not supported in the default build.
Rely strictly on the documented public API. Consult the JSDOC comments in the source code for the definitive public interface.
Review your XML input for any structural errors, unclosed tags, invalid characters, or incorrect entity usage. Ensure it's valid XML, not HTML.
For ES Modules: `import { SaxesParser } from 'saxes';`. For CommonJS: `const { SaxesParser } = require('saxes');`.Handle errors in your `parser.on('error', ...)` listener. Saxes throws errors by default; if you catch them, you typically just let the parser continue or stop processing the input, as there's no `resume` mechanism.No dependency data recorded yet.