{"library":"sax","title":"SAX Streaming XML Parser","description":"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.","language":"javascript","status":"maintenance","last_verified":"Sun Apr 19","install":{"commands":["npm install sax"],"cli":null},"imports":["const sax = require('sax');","const parser = sax.parser(strict, options);","const saxStream = sax.createStream(strict, options);"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const sax = require('sax');\nconst stream = require('stream');\n\n// --- Direct Parser Example ---\nconst strictMode = true; // set to false for html-mode\nconst directParser = sax.parser(strictMode);\n\ndirectParser.onerror = function (e) {\n  console.error(\"Direct Parser Error:\", e.message);\n  if (!strictMode) {\n    // In loose mode, clear error and resume to try and continue parsing\n    this._parser.error = null;\n    this._parser.resume();\n  }\n};\ndirectParser.ontext = function (t) {\n  const trimmedText = t.trim();\n  if (trimmedText) console.log(\"Direct Text:\", trimmedText);\n};\ndirectParser.onopentag = function (node) {\n  console.log(\"Direct Open Tag:\", node.name, \"Attributes:\", JSON.stringify(node.attributes));\n};\ndirectParser.onclosetag = function () {\n  console.log(\"Direct Close Tag\");\n};\ndirectParser.onend = function () {\n  console.log(\"Direct Parser End.\\n\");\n};\n\nconsole.log(\"--- Parsing XML directly ---\");\ndirectParser.write('<root><data name=\"example\">Hello</data>, <world/></root>').close();\n\n\n// --- Stream Parser Example ---\nconst streamMode = false; // loose mode for more forgiving parsing\nconst saxStream = sax.createStream(streamMode, {\n  trim: true,\n  normalize: true,\n  lowercase: true\n});\n\nsaxStream.on('error', function (e) {\n  console.error('Stream Error:', e.message);\n  // Crucial for stream to continue if you want to recover after non-fatal errors\n  this._parser.error = null;\n  this._parser.resume();\n});\n\nsaxStream.on('opentag', function (node) {\n  console.log('Stream Open Tag:', node.name, JSON.stringify(node.attributes));\n});\n\nsaxStream.on('text', function (t) {\n  const trimmedText = t.trim();\n  if (trimmedText) console.log('Stream Text:', trimmedText);\n});\n\nsaxStream.on('end', function () {\n  console.log('Stream End.');\n});\n\nconsole.log(\"--- Parsing XML via stream ---\");\n// Simulate a readable stream from a string buffer\nconst xmlContent = Buffer.from('<catalog><book id=\"1\"><title>The Great Book</title></book><book id=\"2\"></book></catalog>');\nconst readableStream = stream.Readable.from(xmlContent);\n\nreadableStream.pipe(saxStream);\n","lang":"javascript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}