Skip to content

@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-expat
sh
yarn add @hpcc-js/wasm-expat
sh
pnpm add @hpcc-js/wasm-expat

Usage ​

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 here

Expat.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.

Classes ​

Interfaces ​

Type Aliases ​

Reference ​

Released under the Apache-2.0 License.