@anhquoc96/fastxml
v0.1.2
Published
DOM-like XML for Node.js and Deno, with incremental streaming parsing.
Downloads
12
Readme
@anhquoc96/fastxml
Portable XML parsing and DOM-style document APIs for Node.js and Deno.
fastxml is the core package in the fastxml family. It supports convenient
tree-based XML processing and incremental parsing for large documents without
runtime-specific XML dependencies.
Installation
npm install @anhquoc96/fastxmlNode.js:
import { parse, loadFile } from "@anhquoc96/fastxml";Deno:
import { parse, parseStream } from "npm:@anhquoc96/fastxml";Parse XML
import { parse } from "@anhquoc96/fastxml";
const document = parse(
`<catalog xmlns="urn:example:catalog"><book id="1">XML</book></catalog>`,
);
const book = document.getElementsByTagNameNS("urn:example:catalog", "book")[0];
console.log(book.textContent); // XML
console.log(book.getAttribute("id")); // 1The DOM API includes documentElement, childNodes, parentNode,
appendChild, removeChild, cloneNode, textContent, attributes,
getElementsByTagName, getElementsByTagNameNS, querySelector, and
querySelectorAll.
String, file, URL, and stream inputs
import { load, loadFile, parseStream, parseUrl } from "@anhquoc96/fastxml";
const fromText = await load({ text: xmlText });
const fromFile = await loadFile("./large.xml");
const fromStream = await parseStream(response.body!);
const fromUrl = await parseUrl("https://example.com/data.xml");parseStream accepts async iterables and Web Streams. File access is provided
through runtime adapters for Node.js and Deno.
Event parsing for large XML
Use events when the application does not need a complete DOM tree:
import { parseFileEvents } from "@anhquoc96/fastxml";
for await (const event of parseFileEvents("large.xml", { preserveWhitespace: false })) {
if (event.type === "startElement" && event.name === "record") {
// Process records incrementally.
}
}Parser options
const document = parse(xmlText, {
preserveWhitespace: false,
strictNamespaces: true,
maxDepth: 256,
maxTokenLength: 16 * 1024 * 1024,
maxAttributes: 1024,
maxEntityExpansion: 8 * 1024 * 1024,
allowDoctype: false,
});External entities are never resolved. Configure explicit limits when processing untrusted or tenant-controlled XML.
Development
npm install
npm run build --workspace @anhquoc96/fastxml
npm run typecheck --workspace @anhquoc96/fastxmlLicense
MIT License. See the package LICENSE file.
