@eksml/xml

中文文档
0.3.0发布于 2026/10/20MITTypeScriptES Module构建来源已验证近 30 天下载 1.2万

Fast, lightweight XML/HTML parser, serializer, and streaming toolkit

原始使用文档
文档目录8 节
  1. Installation
  2. Parsing
  3. Writing
  4. Streaming
  5. Documentation
  6. Benchmarks
  7. Acknowledgments
  8. License
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

版本与发布记录

@eksml/xml 共发布 9 个版本,首次发布于 2026/04/06;过去 12 个月发布了 9 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.3.0latest2026/10/2024 天前
0.2.02026/07/203 个月前
0.1.62026/07/203 个月前
0.1.52026/07/203 个月前
0.1.42026/07/193 个月前
0.1.32026/07/193 个月前

关于 @eksml/xml 的常见问题

@eksml/xml 是什么?

Fast, lightweight XML/HTML parser, serializer, and streaming toolkit

如何安装 @eksml/xml?

在项目目录运行 npm install @eksml/xml,或使用 pnpm add / yarn add / bun add。

@eksml/xml 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@eksml/xml 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@eksml/xml 可以商用吗?

@eksml/xml 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@eksml/xml 的下载量有多少?

根据 npm 官方下载统计,@eksml/xml 在 2026/10/13 – 2026/11/11 这 30 天内被下载 12,111 次,日均约 404 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@eksml/xml 还在维护吗?

最新版本 0.3.0 发布于 2026/10/20(24 天前);至今共发布 9 个版本,其中过去 12 个月发布了 9 个;首次发布于 2026/04/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。