Skip to content

Repository files navigation

Eksml

Eksml

CI coverage npm license

A fast, lightweight XML/HTML parser, serializer, and streaming toolkit for JavaScript and TypeScript. Import only what you need, tree parsing, SAX streaming, object conversion, or serialization, each as a standalone export.

Built on the same core parsing architecture as tXml by Tobias Nickel, Eksml improves the performance and extends it with additional features.

Try the interactive demo

Installation

pnpm add @eksml/xml
# or: npm install @eksml/xml / yarn add @eksml/xml

Eksml is ESM-only and requires Node.js 18+ (or any modern browser, Deno, or Bun). There are no CommonJS exports.

Parsing

parse() turns an XML string into an array of plain-object nodes:

import { parse } from '@eksml/xml/parser';

const dom = parse('<feed><item id="1">Hello</item></feed>');
// [
//   {
//     tagName: 'feed',
//     attributes: null,
//     children: [
//       { tagName: 'item', attributes: { id: '1' }, children: ['Hello'] },
//     ],
//   },
// ]

Every element is a TNode, { tagName, attributes, children }, and text is a plain string. That's the whole tree model, it's JSON-serializable and safe to clone.

Prefer working with simple objects instead of a tree? Convert directly:

import { lossy } from '@eksml/xml/lossy';

lossy('<user><name>Alice</name><age>30</age></user>');
// => { user: { name: 'Alice', age: '30' } }

See parsing for all options (HTML mode, strict mode, entity decoding, targeted extraction) and converters for the lossy and lossless object formats.

Writing

write() serializes a tree (or a lossy/lossless object, auto-detected) back to a string:

import { write } from '@eksml/xml/writer';

write(dom);
// => '<feed><item id="1">Hello</item></feed>'

write(dom, { pretty: true });
// => '<feed>\n  <item id="1">Hello</item>\n</feed>'

write({ user: { name: 'Alice' } });
// => '<user><name>Alice</name></user>'

See writing for pretty-printing, entity encoding, HTML output, and validation options.

Streaming

For documents too large to hold in memory, or data arriving over the network, XmlParseStream is a standard web TransformStream that emits parsed subtrees as they complete:

import { XmlParseStream } from '@eksml/xml/stream';

const response = await fetch('/feed.xml');
const nodes = response.body
  .pipeThrough(new TextDecoderStream())
  .pipeThrough(new XmlParseStream({ select: 'item' }));

for await (const item of nodes) {
  console.log(item.tagName); // each <item> as soon as it closes
}

The select option emits matching elements individually instead of waiting for the whole document. Works in browsers, Node.js 18+, Deno, and Bun; Node streams bridge in with Readable.toWeb().

If you want raw events instead of trees (or maximum throughput), there's also an EventEmitter-style SAX parser:

import { createSaxParser } from '@eksml/xml/sax';

const parser = createSaxParser();
parser.on('openTag', (tagName, attributes) => console.log(tagName));
parser.write('<feed><item id="1">');
parser.write('Hello</item></feed>');
parser.close();

See web streams and SAX parser for options, output formats, and Node stream interop.

Documentation

Topic Covers
Parsing parse(), all options, the TNode tree model, strict mode, entity decoding, targeted extraction
Writing write(), pretty-printing, entity encoding, validation, input auto-detection
Web streams XmlParseStream, the select option, output formats, chunk buffering, Node interop
SAX parser createSaxParser(), events, dynamic handlers, using with Node streams
Converters lossy(), lossless(), shape rules, round-tripping back to XML
Utilities filter(), getElementById(), getElementsByClassName(), type guards, HTML constants
HTML support What html: true does in each API, and where Eksml stops short of a spec-compliant HTML parser
Security Prototype pollution and circular reference protections, why billion laughs and XXE don't apply
Limitations No DTD/schema validation, no namespace resolution, no XPath, and the workarounds

Benchmarks

Eksml is consistently the fastest JavaScript library at DOM parsing, SAX streaming, and raw tokenization, and leads serialization with validate: false. Full per-fixture results: BENCHMARKS.md.

There is also an interactive benchmark that runs in your browser. It is a separate suite from BENCHMARKS.md: it only includes libraries that run on the web, and uses its own fixtures (or XML you paste in), so its numbers are not directly comparable.

Acknowledgments

Eksml's DOM parser is built on the work of Tobias Nickel and his tXml library. The core parsing architecture, a single-pass, position-tracking string scanner that builds the tree as it goes, is what makes both libraries so fast. Thank you, Tobias, for the elegant approach that made all of this possible.

License

MIT

About

Fast XML Parsing & Writing

Resources

Contributing

Security policy

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages