{"library":"sax-ts","title":"sax-ts: Event-driven XML/HTML Parser","description":"sax-ts is an event-driven SAX-style parser for XML and HTML, fully implemented in TypeScript. It is designed with Deno in mind, ensuring browser compatibility, and is also available for Node.js via npm and JSR. The current stable version is 1.2.13, with a release cadence that includes recent point releases for fixes and platform support. Key differentiators include its TypeScript-first approach, memory efficiency for handling large XML documents (e.g., '80 GB' parsing without burning a laptop), and its ability to robustly parse both well-formed and 'mostly-ok-but-kinda-broken' XML documents often found in feeds like RSS. Forked from `sax-js` at version 1.2.9, it aims to provide a modern, type-safe alternative across multiple JavaScript runtimes, supporting strict XML parsing and more forgiving HTML parsing.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install sax-ts"],"cli":null},"imports":["import { SAXParser } from 'sax-ts';","import { SAXParser } from 'https://deno.land/x/sax_ts@1.2.13/mod.ts';","import { SAXParser } from '@maxim-mazurok/sax_ts';","import { EVENTS } from 'sax-ts';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { SAXParser, EVENTS } from 'sax-ts';\n\n// Example XML string to parse\nconst xmlString = `\n<bookstore xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n  <book category=\"cooking\">\n    <title lang=\"en\">Everyday Italian</title>\n    <author>Giada De Laurentiis</author>\n    <year>2205</year>\n    <price>30.00</price>\n  </book>\n  <book category=\"children\">\n    <title lang=\"en\">Harry Potter</title>\n    <author>J K. Rowling</author>\n    <year>2005</year>\n    <price>29.99</price>\n  </book>\n  <book category=\"web\">\n    <title lang=\"en\">Learning XML</title>\n    <author>Erik T. Ray</author>\n    <year>2003</year>\n    <price>39.95</price>\n  </book>\n</bookstore>\n`;\n\nconst strict: boolean = false; // Set to true for strict XML parsing; false for more permissive (HTML-like) parsing\nconst options = {\n  trim: true,        // Trim text and comment nodes\n  normalize: true,   // Turn any whitespace into a single space\n  lowercase: true,   // Lowercase tag and attribute names in loose mode\n  xmlns: true,       // Support namespaces\n  position: true,    // Track line/col/position\n  strictEntities: true // Only parse predefined XML entities: &amp; &apos; &gt; &lt; &quot;\n};\n\nconst parser = new SAXParser(strict, options);\n\nlet currentTag: string | null = null;\nlet currentText: string = '';\nconst parsedBooks: any[] = [];\nlet currentBook: any = {};\n\nparser.onerror = function (e: Error) {\n  console.error(`Parser error: ${e.message}`);\n  // Crucially, you must clear the error and resume to continue parsing.\n  (this as SAXParser).error = null;\n  (this as SAXParser).resume();\n};\n\nparser.onopentag = function (node) {\n  currentTag = node.name;\n  if (node.name === 'book') {\n    currentBook = { attributes: node.attributes, content: {} };\n  }\n};\n\nparser.onclosetag = function (tagName) {\n  if (currentTag && currentText.trim()) {\n    if (currentBook.content) {\n      currentBook.content[currentTag] = currentText.trim();\n    }\n  }\n  currentText = '';\n  currentTag = null;\n  if (tagName === 'book') {\n    parsedBooks.push(currentBook);\n    currentBook = {};\n  }\n};\n\nparser.ontext = function (t) {\n  if (currentTag) {\n    currentText += t;\n  }\n};\n\nparser.onend = function () {\n  console.log('XML parsing completed.');\n  console.log('Parsed Books:', JSON.stringify(parsedBooks, null, 2));\n};\n\nconsole.log(`Starting XML parsing. Supported events: ${EVENTS.join(', ')}`);\nparser.write(xmlString).close();\n","lang":"typescript","description":"This quickstart demonstrates how to instantiate `SAXParser`, configure its strictness and options, register event handlers for common XML elements (tags, text, errors), and parse a multi-line XML string to extract structured data. It highlights the event-driven nature of the parser and proper error handling.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}