{"id":13673283,"url":"https://github.com/Maxim-Mazurok/sax-ts","last_synced_at":"2025-04-28T09:30:28.928Z","repository":{"id":48525686,"uuid":"169980607","full_name":"Maxim-Mazurok/sax-ts","owner":"Maxim-Mazurok","description":"SAX-style (Simple API for XML) parser in TypeScript","archived":false,"fork":true,"pushed_at":"2024-03-17T05:56:44.000Z","size":627,"stargazers_count":19,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-18T06:10:48.020Z","etag":null,"topics":["contributions-welcome","deno","help-wanted","node","node-js","node-module","nodejs","npm","npm-module","npm-package","sax","sax-parser","typescript","xml","xml-parser"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"isaacs/sax-js","license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Maxim-Mazurok.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-02-10T13:23:13.000Z","updated_at":"2025-03-25T12:26:09.000Z","dependencies_parsed_at":"2023-01-23T09:31:22.409Z","dependency_job_id":null,"html_url":"https://github.com/Maxim-Mazurok/sax-ts","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Maxim-Mazurok%2Fsax-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Maxim-Mazurok%2Fsax-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Maxim-Mazurok%2Fsax-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Maxim-Mazurok%2Fsax-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Maxim-Mazurok","download_url":"https://codeload.github.com/Maxim-Mazurok/sax-ts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251284836,"owners_count":21564675,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["contributions-welcome","deno","help-wanted","node","node-js","node-module","nodejs","npm","npm-module","npm-package","sax","sax-parser","typescript","xml","xml-parser"],"created_at":"2024-08-02T10:00:32.619Z","updated_at":"2025-04-28T09:30:27.918Z","avatar_url":"https://github.com/Maxim-Mazurok.png","language":"JavaScript","funding_links":[],"categories":["Modules","Uncategorized","基础设施"],"sub_categories":["XML","Uncategorized","Online Playgrounds","Assistants","Deno 源"],"readme":"# sax-ts 📦\n\n**Simple API for XML in TypeScript**\n\n[![CI status](https://github.com/Maxim-Mazurok/sax-ts/actions/workflows/test.yml/badge.svg)](https://github.com/Maxim-Mazurok/sax-ts/actions/workflows/test.yml)\n[![License](https://img.shields.io/badge/license-ISC-brightgreen.svg)](https://github.com/Maxim-Mazurok/sax-ts/blob/master/LICENSE.md)\n[![NPM](https://img.shields.io/npm/v/sax-ts.svg)](https://www.npmjs.com/package/sax-ts)\n[![DenoLib](https://denolib.com/badge?scope=Maxim-Mazurok\u0026repo=sax-ts)](https://github.com/denolib)\n[![Deno Third Party Modules](https://shield.deno.dev/x/sax_ts)](https://deno.land/x/sax_ts)\n[![JSR](https://jsr.io/badges/@maxim-mazurok/sax-ts)](https://jsr.io/@maxim-mazurok/sax-ts)\n\n\nA [SAX](https://en.wikipedia.org/wiki/Simple_API_for_XML)-style parser for XML\nand HTML.\n\nDesigned with [deno](https://deno.land/) in mind, so it's **browser compatible**\n\n## What This Is\n\n- A very simple tool to parse through an XML string.\n- A handy way to deal with RSS and other mostly-ok-but-kinda-broken XML\n  docs.\n- A perfect way to parse 80 GB of XML data and don't burn your laptop :)\n\n## Usage\n\n### Deno\n```typescript\nimport { SAXParser } from 'https://unpkg.com/sax-ts@1.2.8/src/sax.ts';\n// for semver, use \"@%5E1.2.8\", which is the urlencoded version of \"@^1.2.8\"\nconst strict: boolean = true; // change to false for HTML parsing\nconst options: {} = {}; // refer to \"Arguments\" section\nconst parser = new SAXParser(strict, options);\n\nparser.onerror = function (e) {\n  // an error happened.\n  console.error(e);\n};\nparser.ontext = function (t) {\n  // got some text.  t is the string of text.\n  console.log('onText: ', t)\n};\nparser.onopentag = function (node) {\n  // opened a tag.  node has \"name\" and \"attributes\"\n  console.log('onOpenTag: ', node)\n};\nparser.onattribute = function (attr) {\n  // an attribute.  attr has \"name\" and \"value\"\n  console.log('onAttribute: ', attr)\n};\nparser.onend = function () {\n  // parser stream is done, and ready to have more stuff written to it.\n  console.warn('end of XML')\n};\n\nparser.write('\u003cxml\u003eHello, \u003cwho name=\"world\"\u003eworld\u003c/who\u003e!\u003c/xml\u003e').close();\n```\n\n\n## Arguments\n\nPass the following arguments to the parser function. All are optional.\n\n`strict` - Boolean. Disabled \"forgiving\" mode. Default: `false`.\n\n`options` - Object bag of settings regarding string formatting. All default to\n`false`.\n\nSettings supported:\n\n- `trim` - Boolean. Whether or not to trim text and comment nodes.\n- `normalize` - Boolean. If true, then turn any whitespace into a single\n  space.\n- `lowercase` - Boolean. If true, then lowercase tag names and attribute names\n  in loose mode, rather than uppercasing them.\n- `xmlns` - Boolean. If true, then namespaces are supported.\n- `position` - Boolean. If false, then don't track line/col/position.\n- `strictEntities` - Boolean. If true, only parse [predefined XML\n  entities](http://www.w3.org/TR/REC-xml/#sec-predefined-ent)\n  (`\u0026amp;`, `\u0026apos;`, `\u0026gt;`, `\u0026lt;`, and `\u0026quot;`)\n\n## Methods\n\n`write` - Write bytes onto the stream. You don't have to do this all at\nonce. You can keep writing as much as you want.\n\n`close` - Close the stream. Once closed, no more data may be written until\nit is done processing the buffer, which is signaled by the `end` event.\n\n`resume` - To gracefully handle errors, assign a listener to the `error`\nevent. Then, when the error is taken care of, you can call `resume` to\ncontinue parsing. Otherwise, the parser will not continue while in an error\nstate.\n\n## Events\n\nAll events emit with a single argument. To listen to an event, assign a\nfunction to `on\u003ceventname\u003e`. Functions get executed in the this-context of\nthe parser object. The list of supported events are also in the exported\n`EVENTS` array.\n\n`error` - Indication that something bad happened. The error will be hanging\nout on `parser.error`, and must be deleted before parsing can continue. By\nlistening to this event, you can keep an eye on that kind of stuff. Note:\nthis happens *much* more in strict mode. Argument: instance of `Error`.\n```javascript\n//TODO: currently `error` is protected, need to expose it to user somehow.\n```\n\n`text` - Text node. Argument: string of text.\n\n`doctype` - The `\u003c!DOCTYPE` declaration. Argument: doctype string.\n\n`processinginstruction` - Stuff like `\u003c?xml foo=\"blerg\" ?\u003e`. Argument:\nobject with `name` and `body` members. Attributes are not parsed, as\nprocessing instructions have implementation dependent semantics.\n\n`sgmldeclaration` - Random SGML declarations. Stuff like `\u003c!ENTITY p\u003e`\nwould trigger this kind of event. This is a weird thing to support, so it\nmight go away at some point. SAX isn't intended to be used to parse SGML,\nafter all.\n\n`opentagstart` - Emitted immediately when the tag name is available,\nbut before any attributes are encountered.  Argument: object with a\n`name` field and an empty `attributes` set.  Note that this is the\nsame object that will later be emitted in the `opentag` event.\n\n`opentag` - An opening tag. Argument: object with `name` and `attributes`.\nIn non-strict mode, tag names are uppercased, unless the `lowercase`\noption is set.  If the `xmlns` option is set, then it will contain\nnamespace binding information on the `ns` member, and will have a\n`local`, `prefix`, and `uri` member.\n\n`closetag` - A closing tag. In loose mode, tags are auto-closed if their\nparent closes. In strict mode, well-formedness is enforced. Note that\nself-closing tags will have `closeTag` emitted immediately after `openTag`.\nArgument: tag name.\n\n`attribute` - An attribute node.  Argument: object with `name` and `value`.\nIn non-strict mode, attribute names are in upper-case, unless the `lowercase`\noption is set.  If the `xmlns` option is set, it will also contains namespace\ninformation.\n\n`comment` - A comment node.  Argument: the string of the comment.\n\n`opencdata` - The opening tag of a `\u003c![CDATA[` block.\n\n`cdata` - The text of a `\u003c![CDATA[` block. Since `\u003c![CDATA[` blocks can get\nquite large, this event may fire multiple times for a single block, if it\nis broken up into multiple `write()`s. Argument: the string of random\ncharacter data.\n\n`closecdata` - The closing tag (`]]\u003e`) of a `\u003c![CDATA[` block.\n\n`opennamespace` - If the `xmlns` option is set, then this event will\nsignal the start of a new namespace binding.\n\n`closenamespace` - If the `xmlns` option is set, then this event will\nsignal the end of a namespace binding.\n\n`end` - Indication that the closed stream has ended.\n\n`ready` - Indication that the stream has reset, and is ready to be written\nto.\n\n`noscript` - In non-strict mode, `\u003cscript\u003e` tags trigger a `\"script\"`\nevent, and their contents are not checked for special xml characters.\nIf you pass `noscript: true`, then this behavior is suppressed.\n\n---\n\n# Disclaimers\n\n## What This Is (probably) Not\n\n- An HTML Parser - That's a fine goal, but this isn't it. It's just XML.\n- A DOM Builder - You can use it to build an object model out of XML, but it\ndoes not do that out of the box.\n- XSLT - No DOM = no querying.\n- 100% Compliant with (some other SAX implementation) - Most SAX\n  implementations are in Java and do a lot more than this does.\n- An XML Validator - It does a little validation when in strict mode, but\n  not much.\n- A Schema-Aware XSD Thing - Schemas are an exercise in fetishistic\n  masochism.\n- A DTD-aware Thing - Fetching DTDs is a much bigger job.\n\n## Regarding `\u003c!DOCTYPE`s and `\u003c!ENTITY`s\n\nThe parser will handle the basic XML entities in text nodes and attribute\nvalues: `\u0026amp; \u0026lt; \u0026gt; \u0026apos; \u0026quot;`. It's possible to define additional\nentities in XML by putting them in the DTD. This parser does not do anything\nwith that.\n\nUnknown entities will fail in strict mode, and in loose mode, will pass\nthrough unmolested.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMaxim-Mazurok%2Fsax-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FMaxim-Mazurok%2Fsax-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMaxim-Mazurok%2Fsax-ts/lists"}