Appearance
@hpcc-js/wasm-expat
This package provides a WebAssembly wrapper around the libexpat library. This provides efficent SAX XML parsing.
Installation
sh
npm install @hpcc-js/wasm-expatsh
yarn add @hpcc-js/wasm-expatsh
pnpm add @hpcc-js/wasm-expatUsage
typescript
import { Expat } from "@hpcc-js/wasm-expat";
const expat = await Expat.load();
const xml = ` \
<root>
<child xxx="yyy">content</child>
</root>
`;
const callback = {
startElement(tag, attrs) { console.log("start", tag, attrs); },
endElement(tag) { console.log("end", tag); },
characterData(content) { console.log("characterData", content); }
};
expat.parse(xml, callback);Try it
[]
Resource management
Expat supports dispose() and TypeScript's using declarations (TypeScript 5.2+ with "ESNext.Disposable" in lib). Leaving the scope is equivalent to calling Expat.unload():
typescript
import { Expat, StackParser } from "@hpcc-js/wasm-expat";
{
using expat = await Expat.load();
const parser = new StackParser();
expat.parse(xml, parser);
} // Expat.unload() runs hereExpat.load() returns a singleton, so disposing it unloads the module for every caller in the current JavaScript context. Disposing an instance that has already been unloaded does nothing.